starting to deompose the parser
This commit is contained in:
parent
96a9cd3270
commit
f628c67188
@ -2,7 +2,7 @@ require 'parslet'
|
||||
|
||||
require "asm/program"
|
||||
require "elf/object_writer"
|
||||
require 'parser/parser'
|
||||
require 'parser/composed'
|
||||
require 'parser/transform'
|
||||
require 'vm/nodes'
|
||||
|
||||
|
9
lib/parser/basic_types.rb
Normal file
9
lib/parser/basic_types.rb
Normal 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
|
@ -1,10 +1,8 @@
|
||||
require_relative "basic_types"
|
||||
|
||||
module Parser
|
||||
class Parser < Parslet::Parser
|
||||
rule(:name) { match('[a-z]').repeat(1).as(:name) >> space? }
|
||||
rule(:number) { match('[0-9]').repeat(1).as(:number) >> space? }
|
||||
rule(:space) { match('\s').repeat(1) }
|
||||
rule(:space?) { space.maybe }
|
||||
class Composed < Parslet::Parser
|
||||
include BasicTypes
|
||||
|
||||
rule(:args) {
|
||||
lparen >>
|
@ -29,5 +29,13 @@ module Parser
|
||||
rule(:func => simple(:func),
|
||||
:params => sequence(:params),
|
||||
: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
|
||||
|
@ -7,7 +7,7 @@ require_relative "helper"
|
||||
class TestNodes < MiniTest::Test
|
||||
|
||||
def setup
|
||||
@parser = Parser::Parser.new
|
||||
@parser = Parser::Composed.new
|
||||
@transform = Parser::Transform.new
|
||||
end
|
||||
|
||||
|
@ -3,7 +3,7 @@ require_relative 'helper'
|
||||
class ParserTest < MiniTest::Test
|
||||
|
||||
def setup
|
||||
@parser = Parser::Parser.new
|
||||
@parser = Parser::Composed.new
|
||||
end
|
||||
|
||||
def check
|
||||
|
Loading…
Reference in New Issue
Block a user