📄 ezexpryacc.y
字号:
/* Grammar for EzGis Expression Parser
NOTES : DON'T FORGET TO MOVE THE GENERATED CONSTANTS TO THE PLACE INDICATED
*/
%{
// Expression parser (c) 2002 EzGis
unit EzExprYacc;
{$I ez_flag.pas}
{$R ezexpryacc.res}
interface
uses
SysUtils, Classes, Dialogs, Windows, EzYaccLib, EzBaseExpr,
EzBase, EzSystem, EzExpressions, EzLib, EzBaseGis;
type
TIdentifierFunctionEvent = procedure( Sender: TObject;
const Group, Identifier: String;
ParameterList: TParameterList;
var Expression: TExpression ) of object;
TExprParser = class(TCustomParser)
private
{ belong to }
FMainExpr: TEzMainExpr;
{ the list of expression as the expression is parsed }
FExprList: TList;
{ for local use }
FTempParams: TParameterList;
FGroupIdent: string;
FIdentifier: string;
FGroupIdentList: TStringList;
FIdentifierList: TStringList;
{ is not this a simple expression ? like TABLE.FIELD
this is used for detecting if giving the same data type to the
result set as the original field }
FIsComplex: Boolean;
{ used to obtain a pair of operators }
Op1, Op2: TExpression;
{ a stacked list of params referencing to FExprList }
FStackedParamCount: TList;
{ the number of parameters for the last function }
FParamCount: Integer;
{ for the case..when..else..end }
FWhenParamList: TParameterList;
FThenParamList: TParameterList;
FElseExpr: TExpression;
{ used when parsing syntax like
VECTOR( [(0,0),(10,10),(20,20),(0,0)] ) }
FVector: TEzVector;
{ used in unknown identifiers }
IDF: TExpression;
FOnIdentifierFunction: TIdentifierFunctionEvent;
{ for saving configuration}
FSavedNegCurrFormat: Byte;
FSavedThousandSeparator: Char;
FSavedDecimalSeparator: Char;
FNumRecords: Integer;
FBufferWidth: Double;
FVectorType: TEzVectorType;
FOrderBy: TStrings;
FDescending: Boolean;
FClosestMax: Integer;
function AddExpression(Expression: TExpression): TExpression;
function GetParamList: TParameterList;
function ForceParamList(Count: Integer): TParameterList;
procedure GetTwoOperators;
procedure GetOneOperator;
procedure AddParam;
function GetString( const s: string ): string;
public
constructor Create(MainExpr: TEzMainExpr);
destructor Destroy; override;
function yyparse : integer; override;
function GetExpression: TExpression;
property IsComplex: Boolean read FIsComplex write FIsComplex;
property OnIdentifierFunction: TIdentifierFunctionEvent read FOnIdentifierFunction write FOnIdentifierFunction;
property Vector: TEzVector read FVector;
property OrderBy: TStrings read FOrderBy;
property ClosestMax: Integer read FClosestMax;
end;
//
// The generated constants must be placed here
// HERE !!!!
//
implementation
(*----------------------------------------------------------------------------*)
constructor TExprParser.Create(MainExpr: TEzMainExpr);
begin
inherited Create;
FMainExpr:= MainExpr; { belongs to }
FExprList:= TList.Create;
FStackedParamCount:= TList.Create;
FGroupIdentList:= TStringList.create;
FIdentifierList:= TStringList.create;
FVector:= TEzVector.Create(10);
FOrderBy:= TStringList.Create;
{ save configuration }
FSavedNegCurrFormat:= NegCurrFormat;
FSavedThousandSeparator:= ThousandSeparator;
FSavedDecimalSeparator:= DecimalSeparator;
NegCurrFormat:= 1;
ThousandSeparator:= ',';
DecimalSeparator:= '.';
end;
destructor TExprParser.Destroy;
var
I: Integer;
begin
for I:= 0 to FExprList.Count-1 do
TObject(FExprList[I]).Free;
FExprList.Free;
if FWhenParamList<>nil then
FWhenParamList.Free;
if FThenParamList<>nil then
FThenParamList.Free;
if FElseExpr<>nil then
FElseExpr.Free;
FStackedParamCount.Free;
FGroupIdentList.free;
FIdentifierList.free;
FVector.Free;
FOrderBy.Free;
{ restore configuration }
NegCurrFormat:= FSavedNegCurrFormat;
ThousandSeparator:= FSavedThousandSeparator;
DecimalSeparator:= FSavedDecimalSeparator;
inherited Destroy;
end;
function TExprParser.GetString( const s: string ): string;
begin
result:= copy( s, 2, length(s) - 2 );
end;
{ this function returns the final expression obtained }
function TExprParser.GetExpression: TExpression;
begin
Result:= nil;
if FExprList.Count <> 1 then Exit;
Result := TExpression( FExprList[0] );
FExprList.Delete( 0 );
end;
function TExprParser.GetParamList: TParameterList;
var
I: Integer;
NumParams: Integer;
begin
Result:= nil;
if FStackedParamCount.Count=0 then
NumParams:= 0
else
begin
NumParams:= Longint(FStackedParamCount[FStackedParamCount.Count-1]);
FStackedParamCount.Delete(FStackedParamCount.Count-1);
end;
if (FExprList.Count=0) or (NumParams=0) or (NumParams>FExprList.Count) then Exit;
Result:= TParameterList.Create;
for I:= 0 to NumParams - 1 do
Result.Add(FExprList[FExprList.Count - NumParams + I]);
while NumParams > 0 do
begin
FExprList.Delete(FExprList.Count-1);
Dec(NumParams);
end;
end;
function TExprParser.ForceParamList(Count: Integer): TParameterList;
var
I: Integer;
NumParams: Integer;
begin
Result:= nil;
NumParams:= Count;
if (FExprList.Count=0) or (NumParams=0) or (NumParams>FExprList.Count) then Exit;
Result:= TParameterList.Create;
for I:= 0 to NumParams - 1 do
Result.Add(FExprList[FExprList.Count - NumParams + I]);
while NumParams > 0 do
begin
FExprList.Delete(FExprList.Count-1);
Dec(NumParams);
end;
end;
procedure TExprParser.GetTwoOperators;
begin
Op1:= TExpression( FExprList[FExprList.Count - 2] );
Op2:= TExpression( FExprList[FExprList.Count - 1] );
FExprList.Delete( FExprList.Count - 1 );
FExprList.Delete( FExprList.Count - 1 );
end;
procedure TExprParser.GetOneOperator;
begin
Op1:= TExpression( FExprList[FExprList.Count - 1] );
FExprList.Delete( FExprList.Count - 1 );
end;
procedure TExprParser.AddParam;
begin
FParamCount:= Longint(FStackedParamCount[FStackedParamCount.Count-1]);
Inc(FParamCount);
FStackedParamCount[FStackedParamCount.Count-1]:= Pointer(FParamCount);
end;
function TExprParser.AddExpression( Expression: TExpression ): TExpression;
begin
FExprList.Add( Expression );
FIsComplex:= True;
Result:= Expression;
end;
%}
%start defined_expr
%token _IDENTIFIER
%token _UINTEGER
%token _SINTEGER
%token _NUMERIC
%token _STRING
%token _COMA
%token _LPAREN
%token _RPAREN
%token _LSQUARE
%token _RSQUARE
%token _PERIOD
%token _SEMICOLON
%token _COMMENT
%token _BLANK
%token _TAB
%token _NEWLINE
%left RW_OR RW_XOR RW_AND
%left _EQ _NEQ _GT _LT _GE _LE RW_BETWEEN RW_IN RW_LIKE
%left _PLUS _SUB
%left _DIV RW_DIV _MULT RW_MOD RW_SHL RW_SHR
%right UMINUS /* Negation--unary minus */
%right _EXP /* exponentiation */
%left RW_NOT
%token _ILLEGAL
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -