From bad08be3d8d20fd3703a5400dfc85012339ff2c4 Mon Sep 17 00:00:00 2001 From: Jakob Nybo Nissen Date: Tue, 15 Aug 2023 12:53:07 +0200 Subject: [PATCH] Bump Automa to v1 --- Project.toml | 2 +- src/MathTeXEngine.jl | 3 -- src/parser/parser.jl | 68 +++++++++++++++++++------------------------- 3 files changed, 30 insertions(+), 43 deletions(-) diff --git a/Project.toml b/Project.toml index 760829c..4ed6880 100644 --- a/Project.toml +++ b/Project.toml @@ -17,7 +17,7 @@ UnicodeFun = "1cfade01-22cf-5700-b092-accc4b62d6e1" [compat] AbstractTrees = "0.3, 0.4" -Automa = "0.8" +Automa = "1" DataStructures = "0.18" FreeTypeAbstraction = "0.10" GeometryBasics = "0.4.1" diff --git a/src/MathTeXEngine.jl b/src/MathTeXEngine.jl index 68d301f..ef84e16 100644 --- a/src/MathTeXEngine.jl +++ b/src/MathTeXEngine.jl @@ -8,7 +8,6 @@ using FreeTypeAbstraction using LaTeXStrings using UnicodeFun -using Automa.RegExp: @re_str using DataStructures: Stack using GeometryBasics: Point2f, Rect2f using REPL.REPLCompletions: latex_symbols @@ -20,8 +19,6 @@ import FreeTypeAbstraction: height_insensitive_boundingbox, leftinkbound, rightinkbound, topinkbound, bottominkbound -const re = Automa.RegExp - export TeXExpr, texparse export TeXElement, TeXChar, VLine, HLine, generate_tex_elements export texfont diff --git a/src/parser/parser.jl b/src/parser/parser.jl index aa9a848..98dca15 100644 --- a/src/parser/parser.jl +++ b/src/parser/parser.jl @@ -53,38 +53,31 @@ function show_debug_info(stack, position, data, action_name) show_state(stack, position, data) end -# Super and subscript -super = re"\^" -super.actions[:exit] = [:end_command_builder, :setup_decorated, :begin_super] - -sub = re"_" -sub.actions[:exit] = [:end_command_builder, :setup_decorated, :begin_sub] - -# Groups -lbrace = re"{" -lbrace.actions[:exit] = [:end_command_builder, :begin_group] - -rbrace = re"}" -rbrace.actions[:exit] = [:end_command_builder, :end_group, :end_token] - -# Commands -bslash = re"\\" -bslash.actions[:exit] = [:end_command_builder, :begin_command_builder] - -command_char = re"[A-Za-z]" -command_char.actions[:exit] = [:push_char, :end_token] - -# Characters -space = re" " -space.actions[:exit] = [:end_command_builder, :push_space] -special_char = lbrace | rbrace | bslash | super | sub | command_char | space -other_char = re"." \ special_char -other_char.actions[:exit] = [:end_command_builder, :push_char, :end_token] - -mathexpr = re.rep(special_char | other_char) -mathexpr.actions[:exit] = [:end_command_builder] - -machine = Automa.compile(mathexpr) +machine = let + # Super and subscript + super = onexit!(re"\^", [:end_command_builder, :setup_decorated, :begin_super]) + sub = onexit!(re"_", [:end_command_builder, :setup_decorated, :begin_sub]) + + # Groups + lbrace = onexit!(re"{", [:end_command_builder, :begin_group]) + rbrace = onexit!(re"}", [:end_command_builder, :end_group, :end_token]) + + # Commands + bslash = onexit!(re"\\", [:end_command_builder, :begin_command_builder]) + command_char = onexit!(re"[A-Za-z]", [:push_char, :end_token]) + + # Characters + space = onexit!(re" ", [:end_command_builder, :push_space]) + special_char = lbrace | rbrace | bslash | super | sub | command_char | space + other_char = onexit!( + re"." \ special_char, + [:end_command_builder, :push_char, :end_token] + ) + + mathexpr = onexit!(Automa.rep(special_char | other_char), :end_command_builder) + + Automa.compile(mathexpr) +end current(stack) = first(stack) current_head(stack) = head(current(stack)) @@ -283,22 +276,19 @@ end actions = Dict(actions...) -context = Automa.CodeGenContext() @eval function texparse(data ; showdebug=false) # Allows string to start with _ or ^ if !isempty(data) && (data[1] == '_' || data[1] == '^') data = "{}" * data end - $(Automa.generate_init_code(context, machine)) - p_end = p_eof = lastindex(data) - + $(Automa.generate_init_code(machine)) # Needed to avoid problem with multi bytes unicode chars stack = Stack{Any}() push!(stack, TeXExpr(:expr)) try - $(Automa.generate_exec_code(context, machine, actions)) + $(Automa.generate_exec_code(machine, actions)) catch throw(TeXParseError("unexpected error while parsing", stack, p, data)) end @@ -310,7 +300,7 @@ context = Automa.CodeGenContext() if length(stack) > 1 err = TeXParseError( "end of string reached with unfinished $(current(stack).head)", - stack, p_eof, data) + stack, p, data) throw(err) end @@ -339,4 +329,4 @@ Setting `showdebug` to `true` show a very verbose break down of the parsing. Parse a LaTeXString composed of a single LaTeX math expression into nested TeXExpr. """ -texparse(data::LaTeXString ; showdebug=false) = texparse(data[2:end-1] ; showdebug=showdebug) \ No newline at end of file +texparse(data::LaTeXString ; showdebug=false) = texparse(data[2:end-1] ; showdebug=showdebug)