Sha256: 1650cc219a60fdff1a3c705da0d43418e2d5400e691c51dcf7d92a1ea9d89b1a
Contents?: true
Size: 1.3 KB
Versions: 1
Compression:
Stored size: 1.3 KB
Contents
require_relative './processor_error' require_relative './syntax_error' require_relative './tokens' class MacroProcessor def initialize(params) @params = params end def parse(text) @tokens = Tokens.new(text) @tokens.advance result = words expect(Tokens::EOF) result end private def words result = [] while @tokens.peek != Tokens::EOF result << word end result.join end def word result = @tokens.peek @tokens.advance if result == '~' and accept('{') @tokens.push_back(macro) result = word end result end def macro args = [] loop do args << word break if @tokens.peek == '}' || @tokens.peek == Tokens::EOF end expect('}') execute(args.join.split(' ')) end def execute(args) if args[0] == 'include' return lookup(:filestore).contents_of(args[1]) else return lookup(args[0]) end end def lookup(field_name) value = @params[field_name] || @params[field_name.to_sym] raise ProcessorError.new(field_name) unless value value end def accept(s) if (@tokens.peek == s) @tokens.advance true else false end end def expect(s) return true if accept(s) raise SyntaxError.new(s, @tokens.peek) false end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
ns-macro-processor-0.0.1 | lib/macro_processor.rb |