class Astrapi::Parser

Attributes

lexer[RW]
tokens[RW]

Public Class Methods

new() click to toggle source
# File lib/parser.rb, line 13
def initialize
  @verbose=true
  @lexer=Lexer.new
end

Public Instance Methods

acceptIt() click to toggle source
# File lib/parser.rb, line 26
def acceptIt
  tok=tokens.shift
  say "consuming #{tok.val} (#{tok.kind})"
  tok
end
expect(kind) click to toggle source
# File lib/parser.rb, line 36
def expect kind
  if (actual=showNext.kind)!=kind
    abort "ERROR at #{showNext.pos}. Expecting #{kind}. Got #{actual}"
  else
    return acceptIt()
  end
end
parse(filename) click to toggle source
# File lib/parser.rb, line 18
def parse filename
  str=IO.read(filename)
  @tokens=lexer.tokenize(str)
  @tokens=@tokens.select{|tok| tok.kind!=:comment}
  pp @tokens if @verbose
  parseModule
end
parseAttr() click to toggle source
# File lib/parser.rb, line 74
def parseAttr
  indent "parseAttr"
  expect :attr
  name=Identifier.new(expect :identifier)
  expect :arrow
  type=parseAttrType
  dedent
  Attr.new(name,type)
end
parseAttrType() click to toggle source
# File lib/parser.rb, line 84
def parseAttrType
  indent "parseAttrType"
  if showNext.is_a? [:IDENT,:FLOAT,:INT,:STRING]
    type=Type.new(Identifier.new(acceptIt))
  else
    type=Type.new(Identifier.new(expect :identifier))
  end
  if showNext.is_a? :lbrack
    acceptIt
    expect :rbrack
    type=ArrayOf.new(type)
  end
  dedent
  return type
end
parseClass() click to toggle source
# File lib/parser.rb, line 57
def parseClass
  indent "parseClass"
  expect :class
  name=Identifier.new(expect :identifier)
  if showNext.is_a? :lt
    acceptIt
    inheritance= Identifier.new(expect :identifier)
  end
  attrs=[]
  while showNext.is_a? :attr
    attrs << parseAttr
  end
  expect :end
  dedent
  return Astrapi::Klass.new(name,inheritance,attrs)
end
parseModule() click to toggle source
# File lib/parser.rb, line 44
def parseModule
  indent "parseModule"
  expect :module
  name=Identifier.new(expect :identifier)
  classes=[]
  while showNext.is_a? :class
    classes << parseClass
  end
  expect :end
  dedent
  return Astrapi::Module.new(name,classes)
end
showNext() click to toggle source
# File lib/parser.rb, line 32
def showNext
  tokens.first
end