qsp_expression.mly 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. | k = FUNCTION_NOARGS
  45. {
  46. (Analyzer.Expression.function_ $loc k [])
  47. }
  48. literal:
  49. | v = LITERAL { Qsp_syntax.T.Text v }
  50. | e = delimited(ENTER_EMBED, expression, LEAVE_EMBED)
  51. { Qsp_syntax.T.Expression e }
  52. unary_operator:
  53. | OBJ
  54. | NO { T.No }
  55. | MINUS { T.Neg }
  56. | PLUS { T.Add }
  57. %inline binary_operator:
  58. | EQUAL { T.Eq }
  59. | LT GT { T.Neq }
  60. | EXCLAMATION { T.Neq }
  61. | PLUS { T.Plus }
  62. | MINUS { T.Minus }
  63. | STAR { T.Product }
  64. | DIV { T.Div }
  65. | MOD { T.Mod }
  66. | GT { T.Gt }
  67. | LT { T.Lt }
  68. | AND { T.And }
  69. | GT EQUAL { T.Gte }
  70. | LT EQUAL { T.Lte }
  71. | EQUAL GT { T.Gte }
  72. | EQUAL LT { T.Lte }
  73. | OR { T.Or }
  74. (** Declare a variable, either in the assignation (let var = …) or as a
  75. reference is an expression
  76. *)
  77. %public variable:
  78. | name = IDENT
  79. brackets = delimited(L_BRACKET, expression?, R_BRACKET)?
  80. {
  81. let index = match brackets with
  82. | None ->
  83. (* No declaration, consider index at 0 *)
  84. None
  85. | Some other -> other in
  86. Qsp_syntax.S.{ pos = $loc ; name ; index; local = false }
  87. }