Build NFA for each token pattern

This commit is contained in:
josh committed 2021-05-17 22:57:18 -04:00
1 parent cf8718b69c
commit 791340b292
3 files changed
+11 -19

No files matched your search

+8 -7
View File
@@ -21,10 +21,10 @@ module Imbecile
@modulename = $1
elsif line =~ /^\s*class\s+(\S+)$/
@classname = $1
elsif line =~ /^\s*token\s+(\S+)(?:\s+(.*))?$/
name, expr = $1, $2
if expr == ""
expr = name
elsif line =~ /^\s*token\s+(\S+)(?:\s+(\S+))?$/
name, pattern = $1, $2
if pattern.to_s == ""
pattern = name
end
unless name =~ /^[a-zA-Z_][a-zA-Z_0-9]*$/
raise Error.new("Invalid token name #{name} on line #{line_number}")
@@ -32,15 +32,16 @@ module Imbecile
if @tokens[name]
raise Error.new("Duplicate token name #{name} on line #{line_number}")
end
@tokens[name] = expr
@tokens[name] = {pattern: pattern}
else
raise Error.new("Unexpected input on line #{line_number}: #{line}")
end
end
# Build NFA from each token expression.
@tokens.transform_values! do |expr|
LexerNFA.new(expr)
@tokens.each do |token_name, token_def|
token_def[:regex] = Regex.new(token_def[:pattern])
token_def[:nfa] = token_def[:regex].parser.unit.to_nfa
end
end
+2 -12
View File
@@ -1,20 +1,10 @@
module Imbecile
class Regex
class State
attr_accessor :accepting
def initialize
@transitions = []
end
def add_transition(character_range, state)
@transitions << [character_range, state]
end
end
attr_accessor :parser
def initialize(pattern)
parser = Parser.new(pattern)
@parser = Parser.new(pattern)
end
end