1a8782179be8 — orbitz NA 7 years ago
Merged in refactor-move-trim-around (pull request #3)

REFACTOR Move trim as the first part of a replacement

Approved-by: Samuel Rivas <samuelrivas@gmail.com>
5 files changed, 33 insertions(+), 37 deletions(-)

M README.org
M src/snabela/snabela.ml
M src/snabela/snabela_lexer.ml
M test_data/foo.tmpl
M tests/snabela/test.ml
M README.org +2 -2
@@ 206,7 206,7 @@ Template:
 @name@ has a minimum age of @min_age@.
 @#?guest_list-@
   Guest list:
-  @#-guest_list-@
+  @-#guest_list-@
     @name@
   @-/guest_list-@
 @/guest_list-@

          
@@ 242,7 242,7 @@ End of the world party party has a minim
 #+END_EXAMPLE
 
 *** Comments
-A template can have a comments.  Comments start with ~@%~ or ~@%-~ and can
+A template can have a comments.  Comments start with ~@%~ or ~@-%~ and can
 contain any character other than ~@~.  A command ends with ~@~ or ~-@~.
 
 *** Transformers

          
M src/snabela/snabela.ml +6 -21
@@ 60,33 60,18 @@ module Template = struct
   let apply_trims tokens =
     let open Snabela_lexer.Token in
     let rec at acc = function
-      | String s::At ln::List::Test::Left_trim::xs ->
-        (* @#?- ... *)
-        at (Test::List::At ln::String (trim_trailing_ws s)::acc) xs
-      | String s::At ln::List::Left_trim::xs ->
-        (* @#- ... *)
-        at (List::At ln::String (trim_trailing_ws s)::acc) xs
-      | String s::At ln::Test::Left_trim::xs ->
-        (* @?- ... *)
-        at (Test::At ln::String (trim_trailing_ws s)::acc) xs
-      | String s::At ln::Neg_test::Left_trim::xs ->
-        (* @!- ... *)
-        at (Neg_test::At ln::String (trim_trailing_ws s)::acc) xs
       | String s::At ln::Left_trim::xs ->
-        (* @- ... *)
+        (* @-... *)
         at (At ln::String (trim_trailing_ws s)::acc) xs
-      | String s::At ln::Comment::Left_trim::xs ->
-        (* @%- ... *)
-        at (Comment::At ln::String (trim_trailing_ws s)::acc) xs
-      | Left_trim::xs ->
-        (* ... - ... *)
-        at acc xs
+      | At ln::Left_trim::xs ->
+        (* Left trim is the first thing *)
+        at (At ln::acc) xs
       | Right_trim::At ln::String s::xs ->
         (* ... -@ ... *)
         at (At ln::acc) (String (trim_leading_ws s)::xs)
-      | Right_trim::xs ->
+      | Right_trim::At ln::xs ->
         (** ... -@ ... *)
-        at acc xs
+        at (At ln::acc) xs
       | x::xs ->
         at (x::acc) xs
       | [] ->

          
M src/snabela/snabela_lexer.ml +10 -10
@@ 48,24 48,24 @@ let rec token ln bldr buf =
   match%sedlex buf with
     | "@@" ->
       token ln (Tb.add Escaped_at bldr) buf
-    | "@%-" ->
-      comment ln (Tb.add_l [At ln; Comment; Left_trim] bldr) buf
+    | "@-%" ->
+      comment ln (Tb.add_l [At ln; Left_trim; Comment] bldr) buf
     | "@%" ->
       comment ln (Tb.add_l [At ln; Comment] bldr) buf
-    | "@#?-" ->
-      replacement ln (Tb.add_l [At ln; List; Test; Left_trim] bldr) buf
-    | "@#-" ->
-      replacement ln (Tb.add_l [At ln; List; Left_trim] bldr) buf
+    | "@-#?" ->
+      replacement ln (Tb.add_l [At ln; Left_trim; List; Test] bldr) buf
+    | "@-#" ->
+      replacement ln (Tb.add_l [At ln; Left_trim; List] bldr) buf
     | "@#?" ->
       replacement ln (Tb.add_l [At ln; List; Test] bldr) buf
     | "@#!" ->
       replacement ln (Tb.add_l [At ln; List; Neg_test] bldr) buf
     | "@#" ->
       replacement ln (Tb.add_l [At ln; List] bldr) buf
-    | "@?-" ->
-      replacement ln (Tb.add_l [At ln; Test; Left_trim] bldr)  buf
-    | "@!-" ->
-      replacement ln (Tb.add_l [At ln; Neg_test; Left_trim] bldr) buf
+    | "@-?" ->
+      replacement ln (Tb.add_l [At ln; Left_trim; Test] bldr)  buf
+    | "@-!" ->
+      replacement ln (Tb.add_l [At ln; Left_trim; Neg_test] bldr) buf
     | "@?" ->
       replacement ln (Tb.add_l [At ln; Test] bldr) buf
     | "@!" ->

          
M test_data/foo.tmpl +3 -1
@@ 8,7 8,7 @@ 
  and has a $ @-cost | money@ cover charge.
 @#?guest_list-@
   Guest list:
-  @#-guest_list-@
+  @-#guest_list-@
     @name@
   @-/guest_list-@
 @/guest_list-@

          
@@ 16,3 16,5 @@ 
   No guests have signed up.
 @/guest_list-@
 @/parties-@
+
+Email joe.blow@@parties.com

          
M tests/snabela/test.ml +12 -3
@@ 82,7 82,7 @@ let test_tokenizer8 =
 @name@ has a minimum age of @min_age@.
 @#?guest_list-@
   Guest list:
-  @#-guest_list-@
+  @-#guest_list-@
     @name@
   @-/guest_list-@
 @/guest_list-@

          
@@ 104,7 104,7 @@ let test_tokenizer8 =
              ; String ".\n"
              ; At 3; List; Test; Key "guest_list"; Right_trim; At 3
              ; String "\n  Guest list:\n  "
-             ; At 5; List; Left_trim; Key "guest_list"; Right_trim; At 5
+             ; At 5; Left_trim; List; Key "guest_list"; Right_trim; At 5
              ; String "\n    "
              ; At 6; Key "name"; At 6
              ; String "\n  "

          
@@ 236,7 236,7 @@ let test_apply9 =
 @name@ has a minimum age of @min_age@.
 @#?guest_list-@
   Guest list:
-  @#-guest_list-@
+  @-#guest_list-@
     @name@
   @-/guest_list-@
 @/guest_list-@

          
@@ 429,6 429,14 @@ let test_apply_fail10 =
        let ret = Snabela.apply compile kv in
        assert (ret = Error (`Missing_key ("name", 3))))
 
+let test_apply_fail11 =
+  Oth.test
+    ~name:"Apply Fail: Malformed Comment"
+    (fun _ ->
+       let template = "@The difference between a valid comment @ and premature closed is subtle@" in
+       let t = Snabela.Template.of_utf8_string template in
+       assert (t = Error (`Invalid_replacement 1)))
+
 let test_transformer1 =
   Oth.test
     ~name:"Transformer: Capitalize"

          
@@ 497,6 505,7 @@ let test =
     ; test_apply_fail8
     ; test_apply_fail9
     ; test_apply_fail10
+    ; test_apply_fail11
     ; test_transformer1
     ; test_transformer2
     ]