literals.ml 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. module Tree = Qsp_syntax.Tree
  2. module Ast = Tree.Ast
  3. module Check = Qsp_syntax.Check
  4. module S = Qsp_syntax.S
  5. module T = Qsp_syntax.T
  6. let _test_instruction = Syntax._test_instruction
  7. let _position = Syntax._position
  8. let result =
  9. [
  10. Tree.Ast.Expression
  11. (Tree.Ast.Literal
  12. ( _position,
  13. [
  14. T.Expression (Tree.Ast.Literal (_position, [ T.Text "key" ]));
  15. T.Text "";
  16. ] ));
  17. ]
  18. (* String and escaped delimiter *)
  19. let nested_squote () = _test_instruction {|'<<''key''>>'|} result
  20. let nested_dquote () = _test_instruction {|"<<""key"">>"|} result
  21. let long_quote () = _test_instruction {|{<<{key}>>}|} result
  22. (* Mix bewteen string enclosing *)
  23. let nested_string_literal2 () = _test_instruction {|"<<'key'>>"|} result
  24. let nested_string_literal4 () = _test_instruction {|{<<'key'>>}|} result
  25. (* The current state shall remain when we are inside an expression. *)
  26. let nested_string_expression1 () = _test_instruction {|'<<(''key'')>>'|} result
  27. let nested_string_expression2 () = _test_instruction {|"<<('key')>>"|} result
  28. (* The block shall also interpreted when inside a comment *)
  29. let nested_comment () =
  30. _test_instruction {|!'<<expr>>'|} [ Tree.Ast.Comment _position ];
  31. _test_instruction {|!"<<expr>>"|} [ Tree.Ast.Comment _position ];
  32. _test_instruction {|!{<<expr>>}|} [ Tree.Ast.Comment _position ]
  33. let comment2 () =
  34. _test_instruction {|!"text <<expr>>"|} [ Tree.Ast.Comment _position ];
  35. _test_instruction {|!{text <<expr>>}|} [ Tree.Ast.Comment _position ];
  36. _test_instruction {|!'text <<expr>>'|} [ Tree.Ast.Comment _position ]
  37. (** A single quote inside a string does not mean we are starting nested string
  38. *)
  39. let direct_text () =
  40. _test_instruction {|"don't"|}
  41. [ Tree.Ast.Expression (Tree.Ast.Literal (_position, [ T.Text "don't" ])) ];
  42. _test_instruction {|'don"t'|}
  43. [ Tree.Ast.Expression (Tree.Ast.Literal (_position, [ T.Text "don\"t" ])) ];
  44. _test_instruction {|'don{t'|}
  45. [ Tree.Ast.Expression (Tree.Ast.Literal (_position, [ T.Text "don{t" ])) ]
  46. let elements_sequence () =
  47. _test_instruction {|"'<<$array[''key'']>>'"|}
  48. [
  49. Tree.Ast.Expression
  50. (Tree.Ast.Literal (_position, [ T.Text "'<<$array[''key'']>>'" ]));
  51. ]
  52. let expression () =
  53. _test_instruction {|'<<iif(var=0,1,0)>>'|}
  54. [
  55. Tree.Ast.Expression
  56. (Tree.Ast.Literal
  57. ( _position,
  58. [
  59. T.Expression
  60. (Tree.Ast.Function
  61. ( _position,
  62. T.Iif,
  63. [
  64. Tree.Ast.BinaryOp
  65. ( _position,
  66. T.Eq,
  67. Tree.Ast.Ident
  68. {
  69. Tree.Ast.pos = _position;
  70. name = "VAR";
  71. index = None;
  72. },
  73. Tree.Ast.Integer (_position, "0") );
  74. Tree.Ast.Integer (_position, "1");
  75. Tree.Ast.Integer (_position, "0");
  76. ] ));
  77. T.Text "";
  78. ] ));
  79. ]
  80. let test =
  81. ( "Literals",
  82. [
  83. Alcotest.test_case "Nested squote" `Quick nested_squote;
  84. Alcotest.test_case "Nested string with literal2" `Quick
  85. nested_string_literal2;
  86. Alcotest.test_case "Nested dquote" `Quick nested_dquote;
  87. Alcotest.test_case "Nested string with literal4" `Quick
  88. nested_string_literal4;
  89. Alcotest.test_case "Nested long_quote" `Quick long_quote;
  90. Alcotest.test_case "Nested string with expression1" `Quick
  91. nested_string_expression1;
  92. Alcotest.test_case "Nested string with expression2" `Quick
  93. nested_string_expression2;
  94. Alcotest.test_case "Nested comment" `Quick nested_comment;
  95. Alcotest.test_case "Comment2" `Quick comment2;
  96. Alcotest.test_case "Quote in string" `Quick direct_text;
  97. Alcotest.test_case "elements_sequence" `Quick elements_sequence;
  98. Alcotest.test_case "expression" `Quick expression;
  99. ] )