Class: Rutile::Lang
- Inherits:
-
Object
- Object
- Rutile::Lang
- Defined in:
- lib/rutile/lang.rb
Instance Method Summary collapse
- #add_file(file, back = false) ⇒ Object
-
#initialize ⇒ Lang
constructor
A new instance of Lang.
- #parse(files) ⇒ Object
- #run(files = ARGV) ⇒ Object
- #tok(regex, symbol = :ignore, &block) ⇒ Object
Constructor Details
#initialize ⇒ Lang
Returns a new instance of Lang.
6 7 8 9 10 11 12 |
# File 'lib/rutile/lang.rb', line 6 def initialize @tokens = [] @nfas = [] @symbols = [] @id = 0 @nfa = nil end |
Instance Method Details
#add_file(file, back = false) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/rutile/lang.rb', line 38 def add_file(file, back = false) if back @parser.file_stack.unshift file else @parser.inc_stack file end end |
#parse(files) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/rutile/lang.rb', line 25 def parse(files) construct_nfa if @nfa.nil? @parser = Parser.new(@nfa, files) @parser.parse.each do |token| id = token.type.min value = @tokens[id].call(token.string) symbol = @symbols[id] if symbol != :ignore #feed it to the grammar thing end end end |
#run(files = ARGV) ⇒ Object
46 47 48 49 50 51 |
# File 'lib/rutile/lang.rb', line 46 def run(files = ARGV) if files.nil? || files == [] files = [STDIN] end parse files end |
#tok(regex, symbol = :ignore, &block) ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/rutile/lang.rb', line 14 def tok(regex, symbol = :ignore, &block) if block.nil? block = Proc.new {|x|} end new_nfa = NFA::to_nfa(regex, @id) @symbols << symbol @nfas << new_nfa @tokens.append(block) @id += 1 end |