| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2010 Jürgen Riegel <[email protected]> * |
| 3 |
* Copyright (c) 2015 Eivind Kvedalen <[email protected]> * |
| 4 |
* * |
| 5 |
* This file is part of the FreeCAD CAx development system. * |
| 6 |
* * |
| 7 |
* This library is free software; you can redistribute it and/or * |
| 8 |
* modify it under the terms of the GNU Library General Public * |
| 9 |
* License as published by the Free Software Foundation; either * |
| 10 |
* version 2 of the License, or (at your option) any later version. * |
| 11 |
* * |
| 12 |
* This library is distributed in the hope that it will be useful, * |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 15 |
* GNU Library General Public License for more details. * |
| 16 |
* * |
| 17 |
* You should have received a copy of the GNU Library General Public * |
| 18 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 19 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 20 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 21 |
* * |
| 22 |
***************************************************************************/ |
| 23 |
|
| 24 |
/* Parser for the FreeCAD Expression language */ |
| 25 |
|
| 26 |
%{ |
| 27 |
#define YYSTYPE App::ExpressionParser::semantic_type |
| 28 |
|
| 29 |
std::stack<FunctionExpression::Function> functions; /**< Function identifier */ |
| 30 |
|
| 31 |
#define yyparse ExpressionParser_yyparse |
| 32 |
#define yyerror ExpressionParser_yyerror |
| 33 |
%} |
| 34 |
|
| 35 |
%token FUNC |
| 36 |
%token ONE |
| 37 |
%token NUM |
| 38 |
%token IDENTIFIER |
| 39 |
%token UNIT USUNIT |
| 40 |
%token INTEGER |
| 41 |
%token CONSTANT |
| 42 |
%token CELLADDRESS |
| 43 |
%token EQ NEQ LT GT GTE LTE |
| 44 |
%token STRING MINUSSIGN PROPERTY_REF |
| 45 |
%token DOCUMENT OBJECT |
| 46 |
%token EXPONENT |
| 47 |
%type <arguments> args |
| 48 |
%type <expr> input unit_num us_building_unit other_unit exp unit_exp indexable |
| 49 |
%type <quantity> UNIT USUNIT |
| 50 |
%type <string> id_or_cell STRING IDENTIFIER CELLADDRESS |
| 51 |
%type <ivalue> INTEGER |
| 52 |
%type <string> PROPERTY_REF |
| 53 |
%type <fvalue> ONE |
| 54 |
%type <fvalue> NUM |
| 55 |
%type <constant> CONSTANT |
| 56 |
%type <expr> num |
| 57 |
%type <expr> range |
| 58 |
%type <path> identifier iden |
| 59 |
%type <component> indexer |
| 60 |
%type <func> FUNC |
| 61 |
%type <string_or_identifier> document |
| 62 |
%type <string_or_identifier> object |
| 63 |
%type <ivalue> integer |
| 64 |
%right '?' ':' |
| 65 |
%left EQ NEQ LT GT GTE LTE |
| 66 |
%left MINUSSIGN '+' |
| 67 |
%left '*' '/' '%' |
| 68 |
%precedence NUM_AND_UNIT |
| 69 |
%left '^' |
| 70 |
%precedence NEG |
| 71 |
%precedence POS |
| 72 |
|
| 73 |
%destructor { delete $$; } num range exp unit_exp indexable |
| 74 |
%destructor { delete $$; } <component> |
| 75 |
%destructor { std::vector<Expression*>::const_iterator i = $$.begin(); while (i != $$.end()) { delete *i; ++i; } } args |
| 76 |
|
| 77 |
%start input |
| 78 |
%% |
| 79 |
|
| 80 |
|
| 81 |
input: exp { ScanResult = $1; valueExpression = true; } |
| 82 |
| unit_exp { ScanResult = $1; unitExpression = true; } |
| 83 |
; |
| 84 |
|
| 85 |
unit_num: num unit_exp %prec NUM_AND_UNIT { $$ = new OperatorExpression(DocumentObject, $1, OperatorExpression::UNIT, $2); } |
| 86 |
| num us_building_unit num us_building_unit %prec NUM_AND_UNIT { $$ = new OperatorExpression(DocumentObject, new OperatorExpression(DocumentObject, $1, OperatorExpression::UNIT, $2), OperatorExpression::ADD, new OperatorExpression(DocumentObject, $3, OperatorExpression::UNIT, $4));} |
| 87 |
; |
| 88 |
|
| 89 |
exp: num { $$ = $1; } |
| 90 |
| unit_num { $$ = $1; } |
| 91 |
| STRING { $$ = new StringExpression(DocumentObject, $1); } |
| 92 |
| identifier { $$ = new VariableExpression(DocumentObject, $1); } |
| 93 |
| MINUSSIGN exp %prec NEG { $$ = new OperatorExpression(DocumentObject, $2, OperatorExpression::NEG, new NumberExpression(DocumentObject, Quantity(-1))); } |
| 94 |
| '+' exp %prec POS { $$ = new OperatorExpression(DocumentObject, $2, OperatorExpression::POS, new NumberExpression(DocumentObject, Quantity(1))); } |
| 95 |
| exp '+' exp { $$ = new OperatorExpression(DocumentObject, $1, OperatorExpression::ADD, $3); } |
| 96 |
| exp MINUSSIGN exp { $$ = new OperatorExpression(DocumentObject, $1, OperatorExpression::SUB, $3); } |
| 97 |
| exp '*' exp { $$ = new OperatorExpression(DocumentObject, $1, OperatorExpression::MUL, $3); } |
| 98 |
| exp '/' exp { $$ = new OperatorExpression(DocumentObject, $1, OperatorExpression::DIV, $3); } |
| 99 |
| exp '%' exp { $$ = new OperatorExpression(DocumentObject, $1, OperatorExpression::MOD, $3); } |
| 100 |
| exp '/' unit_exp { $$ = new OperatorExpression(DocumentObject, $1, OperatorExpression::DIV, $3); } |
| 101 |
| exp '^' exp { $$ = new OperatorExpression(DocumentObject, $1, OperatorExpression::POW, $3); } |
| 102 |
| exp EQ exp { $$ = new OperatorExpression(DocumentObject, $1, OperatorExpression::EQ, $3); } |
| 103 |
| exp NEQ exp { $$ = new OperatorExpression(DocumentObject, $1, OperatorExpression::NEQ, $3); } |
| 104 |
| exp LT exp { $$ = new OperatorExpression(DocumentObject, $1, OperatorExpression::LT, $3); } |
| 105 |
| exp GT exp { $$ = new OperatorExpression(DocumentObject, $1, OperatorExpression::GT, $3); } |
| 106 |
| exp GTE exp { $$ = new OperatorExpression(DocumentObject, $1, OperatorExpression::GTE, $3); } |
| 107 |
| exp LTE exp { $$ = new OperatorExpression(DocumentObject, $1, OperatorExpression::LTE, $3); } |
| 108 |
| indexable { $$ = $1; } |
| 109 |
| FUNC args ')' { $$ = new FunctionExpression(DocumentObject, $1.first, std::move($1.second), $2);} |
| 110 |
| exp '?' exp ':' exp { $$ = new ConditionalExpression(DocumentObject, $1, $3, $5); } |
| 111 |
| '(' exp ')' { $$ = $2; } |
| 112 |
; |
| 113 |
|
| 114 |
num: ONE { $$ = new NumberExpression(DocumentObject, Quantity($1)); } |
| 115 |
| NUM { $$ = new NumberExpression(DocumentObject, Quantity($1)); } |
| 116 |
| INTEGER { $$ = new NumberExpression(DocumentObject, Quantity((double)$1)); } |
| 117 |
| CONSTANT { $$ = new ConstantExpression(DocumentObject, $1.name, Quantity($1.fvalue)); } |
| 118 |
|
| 119 |
args: exp { $$.push_back($1); } |
| 120 |
| range { $$.push_back($1); } |
| 121 |
| args ',' exp { $1.push_back($3); $$ = $1; } |
| 122 |
| args ';' exp { $1.push_back($3); $$ = $1; } |
| 123 |
| args ',' range { $1.push_back($3); $$ = $1; } |
| 124 |
| args ';' range { $1.push_back($3); $$ = $1; } |
| 125 |
; |
| 126 |
|
| 127 |
range: id_or_cell ':' id_or_cell { $$ = new RangeExpression(DocumentObject, $1, $3); } |
| 128 |
; |
| 129 |
|
| 130 |
|
| 131 |
us_building_unit: USUNIT { $$ = new UnitExpression(DocumentObject, $1.scaler, $1.unitStr ); } |
| 132 |
other_unit: UNIT { $$ = new UnitExpression(DocumentObject, $1.scaler, $1.unitStr ); } |
| 133 |
|
| 134 |
unit_exp: other_unit { $$ = $1; } |
| 135 |
| us_building_unit { $$ = $1; } |
| 136 |
| unit_exp '/' unit_exp { $$ = new OperatorExpression(DocumentObject, $1, OperatorExpression::DIV, $3); } |
| 137 |
| unit_exp '*' unit_exp { $$ = new OperatorExpression(DocumentObject, $1, OperatorExpression::MUL, $3); } |
| 138 |
| unit_exp '^' integer { $$ = new OperatorExpression(DocumentObject, $1, OperatorExpression::POW, new NumberExpression(DocumentObject, Quantity((double)$3))); } |
| 139 |
| unit_exp '^' MINUSSIGN integer { $$ = new OperatorExpression(DocumentObject, $1, OperatorExpression::POW, new OperatorExpression(DocumentObject, new NumberExpression(DocumentObject, Quantity((double)$4)), OperatorExpression::NEG, new NumberExpression(DocumentObject, Quantity(-1)))); } |
| 140 |
| '(' unit_exp ')' { $$ = $2; } |
| 141 |
; |
| 142 |
|
| 143 |
integer: INTEGER { $$ = $1; } |
| 144 |
| ONE { $$ = $1; } |
| 145 |
; |
| 146 |
|
| 147 |
id_or_cell |
| 148 |
: IDENTIFIER { $$ = std::move($1); } |
| 149 |
| CELLADDRESS { $$ = std::move($1); } |
| 150 |
; |
| 151 |
|
| 152 |
identifier |
| 153 |
: id_or_cell { $$ = ObjectIdentifier(DocumentObject); $$ << ObjectIdentifier::SimpleComponent($1); } |
| 154 |
| iden { $$ = std::move($1); } |
| 155 |
; |
| 156 |
|
| 157 |
iden |
| 158 |
: '.' STRING '.' id_or_cell { /* Path to property of a sub-object of the current object*/ |
| 159 |
$$ = ObjectIdentifier(DocumentObject,true); |
| 160 |
$$.setDocumentObjectName(DocumentObject,false,ObjectIdentifier::String(std::move($2),true),true); |
| 161 |
$$.addComponent(ObjectIdentifier::SimpleComponent($4)); |
| 162 |
} |
| 163 |
| '.' id_or_cell { /* Path to property of the current document object */ |
| 164 |
$$ = ObjectIdentifier(DocumentObject,true); |
| 165 |
$$.setDocumentObjectName(DocumentObject); |
| 166 |
$$.addComponent(ObjectIdentifier::SimpleComponent($2)); |
| 167 |
} |
| 168 |
| object '.' STRING '.' id_or_cell { /* Path to property of a sub-object */ |
| 169 |
$$ = ObjectIdentifier(DocumentObject); |
| 170 |
$$.setDocumentObjectName(std::move($1), true, ObjectIdentifier::String(std::move($3),true),true); |
| 171 |
$$.addComponent(ObjectIdentifier::SimpleComponent($5)); |
| 172 |
$$.resolveAmbiguity(); |
| 173 |
} |
| 174 |
| object '.' id_or_cell { /* Path to property of a given document object */ |
| 175 |
$$ = ObjectIdentifier(DocumentObject); |
| 176 |
$1.checkImport(DocumentObject); |
| 177 |
$$.addComponent(ObjectIdentifier::SimpleComponent($1)); |
| 178 |
$$.addComponent(ObjectIdentifier::SimpleComponent($3)); |
| 179 |
$$.resolveAmbiguity(); |
| 180 |
} |
| 181 |
| document '#' object '.' id_or_cell { /* Path to property from an external document, within a named document object */ |
| 182 |
$$ = ObjectIdentifier(DocumentObject); |
| 183 |
$$.setDocumentName(std::move($1), true); |
| 184 |
$$.setDocumentObjectName(std::move($3), true); |
| 185 |
$$.addComponent(ObjectIdentifier::SimpleComponent($5)); |
| 186 |
$$.resolveAmbiguity(); |
| 187 |
} |
| 188 |
| document '#' object '.' STRING '.' id_or_cell |
| 189 |
{ $$ = ObjectIdentifier(DocumentObject); |
| 190 |
$$.setDocumentName(std::move($1), true); |
| 191 |
$$.setDocumentObjectName(std::move($3), true, ObjectIdentifier::String(std::move($5),true)); |
| 192 |
$$.addComponent(ObjectIdentifier::SimpleComponent($7)); |
| 193 |
$$.resolveAmbiguity(); |
| 194 |
} |
| 195 |
| iden '.' IDENTIFIER { $$= std::move($1); $$.addComponent(ObjectIdentifier::SimpleComponent($3)); } |
| 196 |
; |
| 197 |
|
| 198 |
indexer |
| 199 |
: '[' exp ']' { $$ = Expression::createComponent($2); } |
| 200 |
| '[' exp ':' ']' { $$ = Expression::createComponent($2,0,0,true); } |
| 201 |
| '[' ':' exp ']' { $$ = Expression::createComponent(0,$3); } |
| 202 |
| '[' ':' ':' exp ']' { $$ = Expression::createComponent(0,0,$4); } |
| 203 |
| '[' exp ':' exp ']' { $$ = Expression::createComponent($2,$4);} |
| 204 |
| '[' exp ':' ':' exp ']' { $$ = Expression::createComponent($2,0,$5); } |
| 205 |
| '[' ':' exp ':' exp ']' { $$ = Expression::createComponent(0,$3,$5); } |
| 206 |
| '[' exp ':' exp ':' exp ']' { $$ = Expression::createComponent($2,$4,$6);} |
| 207 |
; |
| 208 |
|
| 209 |
indexable |
| 210 |
: identifier indexer { $$ = new VariableExpression(DocumentObject,$1); $$->addComponent($2); } |
| 211 |
| indexable indexer { $1->addComponent(std::move($2)); $$ = $1; } |
| 212 |
| indexable '.' IDENTIFIER { $1->addComponent(Expression::createComponent($3)); $$ = $1; } |
| 213 |
; |
| 214 |
|
| 215 |
document |
| 216 |
: STRING { $$ = ObjectIdentifier::String(std::move($1), true); } |
| 217 |
| IDENTIFIER { $$ = ObjectIdentifier::String(std::move($1), false, true);} |
| 218 |
; |
| 219 |
|
| 220 |
object |
| 221 |
: STRING { $$ = ObjectIdentifier::String(std::move($1), true); } |
| 222 |
| id_or_cell { $$ = ObjectIdentifier::String(std::move($1), false);} |
| 223 |
; |
| 224 |
|
| 225 |
%% |