qsp_expression.mly 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. %%
  2. %public arguments(X):
  3. (** This rule allow the difference between elements with a single argument
  4. (when the separator is required) which allow to write a spectif case when
  5. we only have one element.
  6. *)
  7. | hd = X
  8. COMA
  9. tl = separated_nonempty_list(COMA, X)
  10. { hd :: tl }
  11. (** The case where we have nothing is easy to manage here *)
  12. |
  13. { [] }
  14. (** The remaining case (only one argument) is handled outside of this
  15. block: if we have a single argument we don’t have to bother with the
  16. paren, they belongs to the expression.
  17. *)
  18. %inline argument(X):
  19. | a = delimited(L_PAREN, arguments(X), R_PAREN) { a }
  20. | a = X { [ a ] }
  21. (** Declare an expression *)
  22. %public expression:
  23. | ex = delimited(L_PAREN, expression, R_PAREN)
  24. { ex }
  25. | op = unary_operator
  26. expr = expression
  27. { Analyzer.Expression.uoperator $loc op expr }
  28. %prec NO
  29. |
  30. expr1 = expression
  31. op = binary_operator
  32. expr2 = expression
  33. { Analyzer.Expression.boperator $loc op expr1 expr2 }
  34. | v = delimited(TEXT_MARKER, literal*, TEXT_MARKER)
  35. { Analyzer.Expression.literal $loc v }
  36. | i = INTEGER { Analyzer.Expression.integer $loc i }
  37. | v = variable { Analyzer.Expression.ident v }
  38. %prec p_variable
  39. | k = FUNCTION
  40. arg = argument(expression)
  41. {
  42. (Analyzer.Expression.function_ $loc k arg)
  43. }
  44. literal:
  45. | v = LITERAL { Qsp_syntax.T.Text v }
  46. | e = delimited(ENTER_EMBED, expression, LEAVE_EMBED)
  47. { Qsp_syntax.T.Expression e }
  48. unary_operator:
  49. | OBJ
  50. | NO { T.No }
  51. | MINUS { T.Neg }
  52. | PLUS { T.Add }
  53. %inline binary_operator:
  54. | EQUAL { T.Eq }
  55. | LT GT { T.Neq }
  56. | EXCLAMATION { T.Neq }
  57. | PLUS { T.Plus }
  58. | MINUS { T.Minus }
  59. | STAR { T.Product }
  60. | DIV { T.Div }
  61. | MOD { T.Mod }
  62. | GT { T.Gt }
  63. | LT { T.Lt }
  64. | AND { T.And }
  65. | GT EQUAL { T.Gte }
  66. | LT EQUAL { T.Lte }
  67. | EQUAL GT { T.Gte }
  68. | EQUAL LT { T.Lte }
  69. | OR { T.Or }
  70. (** Declare a variable, either in the assignation (let var = …) or as a
  71. reference is an expression
  72. *)
  73. %public variable:
  74. | name = IDENT
  75. brackets = delimited(L_BRACKET, expression?, R_BRACKET)?
  76. {
  77. let index = match brackets with
  78. | None ->
  79. (* No declaration, consider index at 0 *)
  80. None
  81. | Some other -> other in
  82. Qsp_syntax.S.{ pos = $loc ; name ; index }
  83. }