[458] | 1 | %{ |
---|
[642] | 2 | #include "filter_expr_node.hpp" |
---|
[458] | 3 | #include <string> |
---|
| 4 | #include <iostream> |
---|
[493] | 5 | #include "exception.hpp" |
---|
| 6 | |
---|
[642] | 7 | using namespace std; |
---|
| 8 | using namespace xios; |
---|
[493] | 9 | |
---|
[458] | 10 | extern "C" |
---|
| 11 | { |
---|
| 12 | int yyparse(void); |
---|
| 13 | int yylex(void); |
---|
[642] | 14 | int yyerror(const char *s); |
---|
[458] | 15 | } |
---|
| 16 | |
---|
[1601] | 17 | static IFilterExprNode* parsed; |
---|
| 18 | static std::string globalInputText; |
---|
| 19 | static std::string *globalInputText_ptr = 0; |
---|
| 20 | static size_t globalReadOffset = 0; |
---|
| 21 | #pragma omp threadprivate(parsed, globalInputText_ptr, globalReadOffset) |
---|
[642] | 22 | |
---|
| 23 | int readInputForLexer(char* buffer, size_t* numBytesRead, size_t maxBytesToRead) |
---|
| 24 | { |
---|
[1601] | 25 | if(globalInputText_ptr == 0) globalInputText_ptr = new std::string; |
---|
[642] | 26 | size_t numBytesToRead = maxBytesToRead; |
---|
[1601] | 27 | size_t bytesRemaining = (*globalInputText_ptr).length()-globalReadOffset; |
---|
[642] | 28 | size_t i; |
---|
| 29 | if (numBytesToRead > bytesRemaining) numBytesToRead = bytesRemaining; |
---|
[1601] | 30 | for (i = 0; i < numBytesToRead; i++) buffer[i] = (*globalInputText_ptr).c_str()[globalReadOffset + i]; |
---|
[458] | 31 | *numBytesRead = numBytesToRead; |
---|
| 32 | globalReadOffset += numBytesToRead; |
---|
| 33 | return 0; |
---|
[642] | 34 | } |
---|
[458] | 35 | %} |
---|
| 36 | |
---|
| 37 | %union |
---|
| 38 | { |
---|
[642] | 39 | std::string* str; /* symbol table index */ |
---|
| 40 | xios::IScalarExprNode* scalarNode; |
---|
| 41 | xios::IFilterExprNode* filterNode; |
---|
[458] | 42 | }; |
---|
| 43 | |
---|
| 44 | %token <str> NUMBER |
---|
[642] | 45 | %token <str> VAR ID AVERAGE |
---|
[458] | 46 | %token PLUS MINUS TIMES DIVIDE POWER |
---|
[1158] | 47 | %token EQ LT GT LE GE NE |
---|
[458] | 48 | %token LEFT_PARENTHESIS RIGHT_PARENTHESIS |
---|
[1158] | 49 | %token QUESTION_MARK COLON |
---|
[458] | 50 | %token <str> END |
---|
| 51 | |
---|
[1158] | 52 | %nonassoc QUESTION_MARK COLON |
---|
| 53 | %left EQ LT GT LE GE NE |
---|
[458] | 54 | %left PLUS MINUS |
---|
| 55 | %left TIMES DIVIDE |
---|
| 56 | %nonassoc NEG |
---|
| 57 | %right POWER |
---|
| 58 | |
---|
[642] | 59 | %type <scalarNode> Expression |
---|
| 60 | %type <filterNode> Line Field_expr |
---|
[458] | 61 | %start Line |
---|
| 62 | %% |
---|
| 63 | |
---|
| 64 | |
---|
| 65 | Line: |
---|
[642] | 66 | END { /* Nothing to do */ } |
---|
| 67 | | Field_expr END { parsed = $1; } |
---|
| 68 | ; |
---|
[458] | 69 | |
---|
| 70 | Expression: |
---|
[642] | 71 | NUMBER { $$ = new CScalarValExprNode(*$1); delete $1; } |
---|
| 72 | | VAR { $$ = new CScalarVarExprNode(*$1); delete $1; } |
---|
| 73 | | Expression PLUS Expression { $$ = new CScalarBinaryOpExprNode($1, "add", $3); } |
---|
| 74 | | Expression MINUS Expression { $$ = new CScalarBinaryOpExprNode($1, "minus", $3); } |
---|
| 75 | | Expression TIMES Expression { $$ = new CScalarBinaryOpExprNode($1, "mult", $3); } |
---|
| 76 | | Expression DIVIDE Expression { $$ = new CScalarBinaryOpExprNode($1, "div", $3); } |
---|
| 77 | | MINUS Expression %prec NEG { $$ = new CScalarUnaryOpExprNode("neg", $2); } |
---|
| 78 | | Expression POWER Expression { $$ = new CScalarBinaryOpExprNode($1, "pow", $3); } |
---|
[728] | 79 | | Expression EQ Expression { $$ = new CScalarBinaryOpExprNode($1, "eq", $3); } |
---|
| 80 | | Expression LT Expression { $$ = new CScalarBinaryOpExprNode($1, "lt", $3); } |
---|
| 81 | | Expression GT Expression { $$ = new CScalarBinaryOpExprNode($1, "gt", $3); } |
---|
| 82 | | Expression LE Expression { $$ = new CScalarBinaryOpExprNode($1, "le", $3); } |
---|
| 83 | | Expression GE Expression { $$ = new CScalarBinaryOpExprNode($1, "ge", $3); } |
---|
[1158] | 84 | | Expression NE Expression { $$ = new CScalarBinaryOpExprNode($1, "ne", $3); } |
---|
| 85 | | Expression QUESTION_MARK Expression COLON Expression {$$ = new CScalarTernaryOpExprNode($1, "cond", $3, $5);} |
---|
[642] | 86 | | LEFT_PARENTHESIS Expression RIGHT_PARENTHESIS { $$ = $2; } |
---|
| 87 | | ID LEFT_PARENTHESIS Expression RIGHT_PARENTHESIS { $$ = new CScalarUnaryOpExprNode(*$1, $3); delete $1; } |
---|
[458] | 88 | ; |
---|
| 89 | |
---|
| 90 | Field_expr: |
---|
[642] | 91 | ID { $$ = new CFilterFieldExprNode(*$1); delete $1; } |
---|
[643] | 92 | | AVERAGE { $$ = new CFilterTemporalFieldExprNode(*$1); delete $1; } |
---|
[642] | 93 | | Field_expr PLUS Field_expr { $$ = new CFilterFieldFieldOpExprNode($1, "add", $3); } |
---|
| 94 | | Field_expr MINUS Field_expr { $$ = new CFilterFieldFieldOpExprNode($1, "minus", $3); } |
---|
| 95 | | Field_expr TIMES Field_expr { $$ = new CFilterFieldFieldOpExprNode($1, "mult", $3); } |
---|
| 96 | | Field_expr DIVIDE Field_expr { $$ = new CFilterFieldFieldOpExprNode($1, "div", $3); } |
---|
| 97 | | MINUS Field_expr %prec NEG { $$ = new CFilterUnaryOpExprNode("neg", $2); } |
---|
| 98 | | Field_expr POWER Field_expr { $$ = new CFilterFieldFieldOpExprNode($1, "pow", $3); } |
---|
[728] | 99 | | Field_expr EQ Field_expr { $$ = new CFilterFieldFieldOpExprNode($1, "eq", $3); } |
---|
| 100 | | Field_expr LT Field_expr { $$ = new CFilterFieldFieldOpExprNode($1, "lt", $3); } |
---|
| 101 | | Field_expr GT Field_expr { $$ = new CFilterFieldFieldOpExprNode($1, "gt", $3); } |
---|
| 102 | | Field_expr LE Field_expr { $$ = new CFilterFieldFieldOpExprNode($1, "le", $3); } |
---|
| 103 | | Field_expr GE Field_expr { $$ = new CFilterFieldFieldOpExprNode($1, "ge", $3); } |
---|
[1158] | 104 | | Field_expr NE Field_expr { $$ = new CFilterFieldFieldOpExprNode($1, "ne", $3); } |
---|
[642] | 105 | | LEFT_PARENTHESIS Field_expr RIGHT_PARENTHESIS { $$ = $2; } |
---|
[1158] | 106 | | Expression QUESTION_MARK Expression COLON Field_expr {$$ = new CFilterScalarScalarFieldOpExprNode($1, "cond",$3, $5);} |
---|
| 107 | | Expression QUESTION_MARK Field_expr COLON Expression {$$ = new CFilterScalarFieldScalarOpExprNode($1, "cond",$3, $5);} |
---|
| 108 | | Expression QUESTION_MARK Field_expr COLON Field_expr {$$ = new CFilterScalarFieldFieldOpExprNode($1, "cond",$3, $5);} |
---|
| 109 | | Field_expr QUESTION_MARK Expression COLON Expression {$$ = new CFilterFieldScalarScalarOpExprNode($1, "cond",$3, $5);} |
---|
| 110 | | Field_expr QUESTION_MARK Expression COLON Field_expr {$$ = new CFilterFieldScalarFieldOpExprNode($1, "cond",$3, $5);} |
---|
| 111 | | Field_expr QUESTION_MARK Field_expr COLON Expression {$$ = new CFilterFieldFieldScalarOpExprNode($1, "cond",$3, $5);} |
---|
| 112 | | Field_expr QUESTION_MARK Field_expr COLON Field_expr {$$ = new CFilterFieldFieldFieldOpExprNode($1, "cond",$3, $5);} |
---|
[642] | 113 | | Field_expr PLUS Expression { $$ = new CFilterFieldScalarOpExprNode($1, "add", $3); } |
---|
| 114 | | Expression PLUS Field_expr { $$ = new CFilterScalarFieldOpExprNode($1, "add", $3); } |
---|
| 115 | | Field_expr MINUS Expression { $$ = new CFilterFieldScalarOpExprNode($1, "minus", $3); } |
---|
| 116 | | Expression MINUS Field_expr { $$ = new CFilterScalarFieldOpExprNode($1, "minus", $3); } |
---|
| 117 | | Field_expr TIMES Expression { $$ = new CFilterFieldScalarOpExprNode($1, "mult", $3); } |
---|
| 118 | | Expression TIMES Field_expr { $$ = new CFilterScalarFieldOpExprNode($1, "mult", $3); } |
---|
| 119 | | Field_expr DIVIDE Expression { $$ = new CFilterFieldScalarOpExprNode($1, "div", $3); } |
---|
| 120 | | Expression DIVIDE Field_expr { $$ = new CFilterScalarFieldOpExprNode($1, "div", $3); } |
---|
| 121 | | Field_expr POWER Expression { $$ = new CFilterFieldScalarOpExprNode($1, "pow", $3); } |
---|
[728] | 122 | | Field_expr EQ Expression { $$ = new CFilterFieldScalarOpExprNode($1, "eq", $3); } |
---|
| 123 | | Expression EQ Field_expr { $$ = new CFilterScalarFieldOpExprNode($1, "eq", $3); } |
---|
| 124 | | Field_expr LT Expression { $$ = new CFilterFieldScalarOpExprNode($1, "lt", $3); } |
---|
| 125 | | Expression LT Field_expr { $$ = new CFilterScalarFieldOpExprNode($1, "lt", $3); } |
---|
| 126 | | Field_expr GT Expression { $$ = new CFilterFieldScalarOpExprNode($1, "gt", $3); } |
---|
| 127 | | Expression GT Field_expr { $$ = new CFilterScalarFieldOpExprNode($1, "gt", $3); } |
---|
| 128 | | Field_expr LE Expression { $$ = new CFilterFieldScalarOpExprNode($1, "le", $3); } |
---|
| 129 | | Expression LE Field_expr { $$ = new CFilterScalarFieldOpExprNode($1, "le", $3); } |
---|
| 130 | | Field_expr GE Expression { $$ = new CFilterFieldScalarOpExprNode($1, "ge", $3); } |
---|
| 131 | | Expression GE Field_expr { $$ = new CFilterScalarFieldOpExprNode($1, "ge", $3); } |
---|
[1158] | 132 | | Field_expr NE Expression { $$ = new CFilterFieldScalarOpExprNode($1, "ne", $3); } |
---|
| 133 | | Expression NE Field_expr { $$ = new CFilterScalarFieldOpExprNode($1, "ne", $3); } |
---|
[642] | 134 | | ID LEFT_PARENTHESIS Field_expr RIGHT_PARENTHESIS { $$ = new CFilterUnaryOpExprNode(*$1, $3); delete $1; } |
---|
[458] | 135 | ; |
---|
| 136 | %% |
---|
| 137 | |
---|
| 138 | extern "C" |
---|
| 139 | { |
---|
[642] | 140 | int yyerror(const char *s) |
---|
[458] | 141 | { |
---|
[642] | 142 | ERROR("int yyerror(const char *s)", << "Parsing error: " << s << endl); |
---|
[458] | 143 | } |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | namespace xios |
---|
| 147 | { |
---|
[642] | 148 | IFilterExprNode* parseExpr(const string& strExpr) |
---|
[458] | 149 | { |
---|
[1601] | 150 | #pragma omp critical (_parser) |
---|
| 151 | { |
---|
| 152 | if(globalInputText_ptr == 0) globalInputText_ptr = new std::string; |
---|
| 153 | (*globalInputText_ptr).assign (strExpr); |
---|
| 154 | globalReadOffset = 0; |
---|
| 155 | yyparse(); |
---|
| 156 | } |
---|
[642] | 157 | return parsed; |
---|
[458] | 158 | } |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | |
---|