@@ -53,38 +53,31 @@ function show_debug_info(stack, position, data, action_name)
5353 show_state (stack, position, data)
5454end
5555
56- # Super and subscript
57- super = re "\^ "
58- super. actions[:exit ] = [:end_command_builder , :setup_decorated , :begin_super ]
59-
60- sub = re " _"
61- sub. actions[:exit ] = [:end_command_builder , :setup_decorated , :begin_sub ]
62-
63- # Groups
64- lbrace = re " {"
65- lbrace. actions[:exit ] = [:end_command_builder , :begin_group ]
66-
67- rbrace = re " }"
68- rbrace. actions[:exit ] = [:end_command_builder , :end_group , :end_token ]
69-
70- # Commands
71- bslash = re "\\ "
72- bslash. actions[:exit ] = [:end_command_builder , :begin_command_builder ]
73-
74- command_char = re " [A-Za-z]"
75- command_char. actions[:exit ] = [:push_char , :end_token ]
76-
77- # Characters
78- space = re " "
79- space. actions[:exit ] = [:end_command_builder , :push_space ]
80- special_char = lbrace | rbrace | bslash | super | sub | command_char | space
81- other_char = re " ." \ special_char
82- other_char. actions[:exit ] = [:end_command_builder , :push_char , :end_token ]
83-
84- mathexpr = re. rep (special_char | other_char)
85- mathexpr. actions[:exit ] = [:end_command_builder ]
86-
87- machine = Automa. compile (mathexpr)
56+ machine = let
57+ # Super and subscript
58+ super = onexit! (re "\^ " , [:end_command_builder , :setup_decorated , :begin_super ])
59+ sub = onexit! (re " _" , [:end_command_builder , :setup_decorated , :begin_sub ])
60+
61+ # Groups
62+ lbrace = onexit! (re " {" , [:end_command_builder , :begin_group ])
63+ rbrace = onexit! (re " }" , [:end_command_builder , :end_group , :end_token ])
64+
65+ # Commands
66+ bslash = onexit! (re "\\ " , [:end_command_builder , :begin_command_builder ])
67+ command_char = onexit! (re " [A-Za-z]" , [:push_char , :end_token ])
68+
69+ # Characters
70+ space = onexit! (re " " , [:end_command_builder , :push_space ])
71+ special_char = lbrace | rbrace | bslash | super | sub | command_char | space
72+ other_char = onexit! (
73+ re " ." \ special_char,
74+ [:end_command_builder , :push_char , :end_token ]
75+ )
76+
77+ mathexpr = onexit! (Automa. rep (special_char | other_char), :end_command_builder )
78+
79+ Automa. compile (mathexpr)
80+ end
8881
8982current (stack) = first (stack)
9083current_head (stack) = head (current (stack))
@@ -283,22 +276,19 @@ end
283276
284277actions = Dict (actions... )
285278
286- context = Automa. CodeGenContext ()
287279@eval function texparse (data ; showdebug= false )
288280 # Allows string to start with _ or ^
289281 if ! isempty (data) && (data[1 ] == ' _' || data[1 ] == ' ^' )
290282 data = " {}" * data
291283 end
292284
293- $ (Automa. generate_init_code (context, machine))
294- p_end = p_eof = lastindex (data)
295-
285+ $ (Automa. generate_init_code (machine))
296286 # Needed to avoid problem with multi bytes unicode chars
297287 stack = Stack {Any} ()
298288 push! (stack, TeXExpr (:expr ))
299289
300290 try
301- $ (Automa. generate_exec_code (context, machine, actions))
291+ $ (Automa. generate_exec_code (machine, actions))
302292 catch
303293 throw (TeXParseError (" unexpected error while parsing" , stack, p, data))
304294 end
@@ -310,7 +300,7 @@ context = Automa.CodeGenContext()
310300 if length (stack) > 1
311301 err = TeXParseError (
312302 " end of string reached with unfinished $(current (stack). head) " ,
313- stack, p_eof , data)
303+ stack, p , data)
314304 throw (err)
315305 end
316306
@@ -339,4 +329,4 @@ Setting `showdebug` to `true` show a very verbose break down of the parsing.
339329Parse a LaTeXString composed of a single LaTeX math expression into nested
340330TeXExpr.
341331"""
342- texparse (data:: LaTeXString ; showdebug= false ) = texparse (data[2 : end - 1 ] ; showdebug= showdebug)
332+ texparse (data:: LaTeXString ; showdebug= false ) = texparse (data[2 : end - 1 ] ; showdebug= showdebug)
0 commit comments