Skip to content
This repository
Kaspar Schiess March 02, 2011
file 78 lines (60 sloc) 1.409 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
# A more complex parser that illustrates how a compiler might be constructed.
# The parser recognizes strings and integer literals and constructs almost a
# useful AST from the file contents.

require 'pp'

$:.unshift File.dirname(__FILE__) + "/../lib"
require 'parslet'

include Parslet

class LiteralsParser < Parslet::Parser
  rule :space do
    (match '[ ]').repeat(1)
  end
  
  rule :literals do
    (literal >> eol).repeat
  end
  
  rule :literal do
    (integer | string).as(:literal) >> space.maybe
  end
  
  rule :string do
    str('"') >>
    (
      (str('\\') >> any) |
      (str('"').absent? >> any)
    ).repeat.as(:string) >>
    str('"')
  end
  
  rule :integer do
    match('[0-9]').repeat(1).as(:integer)
  end
  
  rule :eol do
    line_end.repeat(1)
  end
  
  rule :line_end do
    crlf >> space.maybe
  end
  
  rule :crlf do
    match('[\r\n]').repeat(1)
  end

  root :literals
end

input_name = File.join(File.dirname(__FILE__), 'simple.lit')
file = File.read(input_name)

parsetree = LiteralsParser.new.parse(file)
  
class Lit < Struct.new(:text)
  def to_s
    text.inspect
  end
end
class StringLit < Lit
end
class IntLit < Lit
  def to_s
    text
  end
end

transform = Parslet::Transform.new do
  rule(:literal => {:integer => simple(:x)}) { IntLit.new(x) }
  rule(:literal => {:string => simple(:s)}) { StringLit.new(s) }
end
  
ast = transform.apply(parsetree)
pp ast
Something went wrong with that request. Please try again.