aboutsummaryrefslogtreecommitdiff
path: root/src/odf/odf_ExpressionParser.mly
diff options
context:
space:
mode:
Diffstat (limited to 'src/odf/odf_ExpressionParser.mly')
-rwxr-xr-xsrc/odf/odf_ExpressionParser.mly95
1 files changed, 95 insertions, 0 deletions
diff --git a/src/odf/odf_ExpressionParser.mly b/src/odf/odf_ExpressionParser.mly
new file mode 100755
index 0000000..6b571a9
--- /dev/null
+++ b/src/odf/odf_ExpressionParser.mly
@@ -0,0 +1,95 @@
+%{
+ open ScTypes
+ module F = Functions
+
+ let u = UTF8.from_utf8string
+
+ let extractColumnNameFromNum (fixed, str) = (fixed, int_of_string str)
+
+%}
+
+%token <string> REAL
+%token <string> NUM
+%token <string> STR
+
+%token <string> LETTERS
+%token <string> IDENT
+
+%token DOLLAR
+%token DOT
+
+%token LPAREN RPAREN
+%token L_SQ_BRACKET R_SQ_BRACKET
+%token PLUS
+%token TIMES
+%token DIVIDE
+%token MINUS
+%token EQ NEQ
+%token LT LE GT GE
+%token EOF
+%token COLON SEMICOLON
+%token POW
+
+%nonassoc EQ NEQ LT LE GT GE
+%left PLUS MINUS
+%left TIMES DIVIDE
+%left POW
+
+%start<ScTypes.expression> value
+
+%%
+
+value:
+ | LETTERS COLON EQ expr EOF {$4}
+
+expr:
+ | num {Value (number ($1))}
+ | MINUS expr {Call (F.sub, [$2])}
+ | PLUS expr {Call (F.add, [$2])}
+
+
+ | L_SQ_BRACKET ref R_SQ_BRACKET {$2}
+
+ | LPAREN expr RPAREN {Expression $2}
+ | STR {Value (string (u $1))}
+
+ (* Mathematical operators *)
+ | expr MINUS expr {Call (F.sub, [$1; $3])}
+ | expr DIVIDE expr {Call (F.div, [$1; $3])}
+ | expr TIMES expr {Call (F.mul, [$1; $3])}
+ | expr PLUS expr {Call (F.add, [$1; $3])}
+ | expr POW expr {Call (F.pow, [$1; $3])}
+
+ (* Comparaison *)
+ | expr EQ expr {Call (F.eq, [$1; $3])}
+ | expr NEQ expr {Call (F.neq, [$1; $3])}
+ | expr LT expr {Call (F.lt, [$1; $3])}
+ | expr GT expr {Call (F.gt, [$1; $3])}
+ | expr LE expr {Call (F.le, [$1; $3])}
+ | expr GE expr {Call (F.ge, [$1; $3])}
+
+ | ident LPAREN separated_list(SEMICOLON, expr) RPAREN { Call (u $1, $3) }
+
+
+ref:
+ | cell {Ref (Cell $1)}
+ | cell COLON cell {Ref (Range ($1, $3))}
+
+cell:
+ | DOT fixed(LETTERS) fixed(NUM){Cell.from_string $2 (extractColumnNameFromNum $3)}
+
+fixed(X):
+ | DOLLAR X {true, $2}
+ | X {false, $1}
+
+num:
+ | REAL {DataType.Num.of_float @@ float_of_string $1}
+ | NUM {DataType.Num.of_int @@ int_of_string $1}
+
+ident:
+ | IDENT { $1 }
+ | text+ { String.concat "" $1 }
+
+text:
+ | LETTERS { $1 }
+ | NUM { $1 }