Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump Automa to v1 #108

Merged
merged 1 commit into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 0 additions & 3 deletions src/MathTeXEngine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
68 changes: 29 additions & 39 deletions src/parser/parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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)
texparse(data::LaTeXString ; showdebug=false) = texparse(data[2:end-1] ; showdebug=showdebug)
Loading