starting to deompose the parser

This commit is contained in:
Torsten Ruger 2014-04-27 16:30:32 +03:00
parent 96a9cd3270
commit f628c67188
6 changed files with 23 additions and 8 deletions

View File

@ -2,7 +2,7 @@ require 'parslet'
require "asm/program" require "asm/program"
require "elf/object_writer" require "elf/object_writer"
require 'parser/parser' require 'parser/composed'
require 'parser/transform' require 'parser/transform'
require 'vm/nodes' require 'vm/nodes'

View File

@ -0,0 +1,9 @@
module Parser
module BasicTypes
include Parslet
rule(:space) { match('\s').repeat(1) }
rule(:space?) { space.maybe }
rule(:name) { match('[a-z]').repeat(1).as(:name) >> space? }
rule(:number) { match('[0-9]').repeat(1).as(:number) >> space? }
end
end

View File

@ -1,10 +1,8 @@
require_relative "basic_types"
module Parser module Parser
class Parser < Parslet::Parser class Composed < Parslet::Parser
rule(:name) { match('[a-z]').repeat(1).as(:name) >> space? } include BasicTypes
rule(:number) { match('[0-9]').repeat(1).as(:number) >> space? }
rule(:space) { match('\s').repeat(1) }
rule(:space?) { space.maybe }
rule(:args) { rule(:args) {
lparen >> lparen >>

View File

@ -29,5 +29,13 @@ module Parser
rule(:func => simple(:func), rule(:func => simple(:func),
:params => sequence(:params), :params => sequence(:params),
:body => simple(:body)) { Vm::FunctionExpression.new(func.name, params, body) } :body => simple(:body)) { Vm::FunctionExpression.new(func.name, params, body) }
#shortcut to get the ast tree for a given string
# optional second arguement specifies a rule that will be parsed (mainly for testing)
def self.ast string , rule = :root
syntax = Parser.new.send(rule).parse(string)
tree = Transform.new.apply(syntax)
tree
end
end end
end end

View File

@ -7,7 +7,7 @@ require_relative "helper"
class TestNodes < MiniTest::Test class TestNodes < MiniTest::Test
def setup def setup
@parser = Parser::Parser.new @parser = Parser::Composed.new
@transform = Parser::Transform.new @transform = Parser::Transform.new
end end

View File

@ -3,7 +3,7 @@ require_relative 'helper'
class ParserTest < MiniTest::Test class ParserTest < MiniTest::Test
def setup def setup
@parser = Parser::Parser.new @parser = Parser::Composed.new
end end
def check def check