Skip to content

Commit

Permalink
More parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
brixen committed Nov 11, 2010
1 parent 608f508 commit c87d4f7
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 50 deletions.
34 changes: 32 additions & 2 deletions lib/poison/bootstrap/parser.rb
@@ -1,6 +1,8 @@
module Poison
class Parser
def initialize
@statements = []
@call_list = []
end

# The #parse method is defined in the parser C extension.
Expand All @@ -16,10 +18,18 @@ def syntax_error
raise Syntax::SyntaxError
end

def statement(statement)
@statements << statement
end

def statements(values)
Syntax::Statements.new values
end

def call_list(expr)
@call_list << expr
end

def nil_kind
Syntax::NilKind.new
end
Expand Down Expand Up @@ -48,12 +58,32 @@ def int(value)
Syntax::Integer.new value.to_i 10
end

def value(value)
Syntax::Value.new value
end

def expr(value)
Syntax::Expression.new value
end

def path(name)
"/" + name
Syntax::Path.new name
end

def pathq(name)
Syntax::PathQuery.new name
end

def query(name)
Syntax::Query.new name
end

def message(name)
name
if name.kind_of? Syntax::Message
name
else
Syntax::Message.new name
end
end

def method_missing(sym, *args)
Expand Down
33 changes: 15 additions & 18 deletions lib/poison/bootstrap/parser/ext/parser.g
Expand Up @@ -34,10 +34,10 @@

poison = -- s:statements end-of-file { $$ = P->ast = PN_AST("statements", s); }

statements = s1:stmt { }
(sep s2:stmt { })*
statements = s1:stmt { $$ = PN_AST("statement", s1); }
(sep s2:stmt { $$ = PN_AST("statement", s2); })*
sep?
| '' { $$ = PN_VAL("nil_kind"); }
| '' { $$ = PN_AST("statement", Qnil); }

stmt = s:sets
( or x:sets { }
Expand Down Expand Up @@ -106,24 +106,21 @@ sign = minus !minus s:sign { }
| wavy s:sign { }
| e:expr { }

expr = ( mminus a:atom { }
| pplus a:atom { }
| a:atom (pplus { }
| mminus { })?) { }
(c:call { })*
{ }
expr = ( a:atom ) { a = PN_AST("call_list", a); }
(c:call { a = PN_AST("call_list", c); })*
{ $$ = PN_AST("expr", a); }

atom = e:value | e:closure | e:table | e:call

call = (n:name { } (v:value | v:table)? |
(v:value | v:table) { })
b:block? { }
call = (n:name { v = Qnil; b = Qnil; } (v:value | v:table)? |
(v:value | v:table) { n = PN_AST("message", Qnil); b = Qnil; })
b:block? { $$ = n; }

name = p:path { }
| quiz ( m:message { }
| p:path { })
name = p:path { $$ = PN_AST("path", p); }
| quiz ( m:message { $$ = PN_AST("query", m); }
| p:path { $$ = PN_AST("pathq", p); })
| !keyword
m:message { }
m:message { $$ = PN_AST("message", m); }

lick-items = i1:lick-item { }
(sep i2:lick-item { })*
Expand All @@ -137,7 +134,7 @@ lick-item = m:message t:table v:loose { }
| m:message { }

loose = value
| v:unquoted { }
| v:unquoted { $$ = PN_AST("value", v); }

closure = t:table? b:block { }
table = table-start s:statements table-end { }
Expand All @@ -147,7 +144,7 @@ lick = lick-start i:lick-items lick-end { }
path = '/' m:message { $$ = PN_AST("path", m); }
message = < utfw+ > - { $$ = PN_AST("message", rb_str_new(yytext, yyleng)); }

value = i:immed - { }
value = i:immed - { $$ = PN_AST("value", i); }
| lick

immed = nil { $$ = PN_VAL("nil_kind"); }
Expand Down
25 changes: 2 additions & 23 deletions lib/poison/bootstrap/syntax.rb
@@ -1,6 +1,8 @@
require 'poison/bootstrap/syntax/node'
require 'poison/bootstrap/syntax/value'
require 'poison/bootstrap/syntax/expression'
require 'poison/bootstrap/syntax/operator'
require 'poison/bootstrap/syntax/message'

module Poison
module Syntax
Expand Down Expand Up @@ -55,17 +57,6 @@ def to_sexp
end
end

class BinaryOperator
def initialize(left, right)
@left = left
@right = right
end

def to_sexp
[sexp_name, @left.to_sexp, @right.to_sexp]
end
end

class Or < BinaryOperator
def sexp_name
:or
Expand Down Expand Up @@ -144,18 +135,6 @@ def sexp_name
end
end

class Message < Node
attr_accessor :name

def initialize(name)
@name = name
end

def to_sexp
[:message, [@name, nil, nil]]
end
end


end
end
37 changes: 37 additions & 0 deletions lib/poison/bootstrap/syntax/message.rb
@@ -0,0 +1,37 @@
module Poison
module Syntax
class Message < Node
attr_accessor :name

def initialize(name)
@name = name
end

def sexp_name
:message
end

def to_sexp
[sexp_name, [@name, nil, nil]]
end
end

class Path < Message
def sexp_name
:path
end
end

class PathQuery < Message
def sexp_name
:path_query
end
end

class Query < Message
def sexp_name
:query
end
end
end
end
24 changes: 24 additions & 0 deletions lib/poison/bootstrap/syntax/operator.rb
@@ -0,0 +1,24 @@
module Poison
module Syntax
class UnaryOperator < Node
def initialize(value)
@value = value
end

def to_sexp
[sexp_name, @value]
end
end

class BinaryOperator < Node
def initialize(left, right)
@left = left
@right = right
end

def to_sexp
[sexp_name, @left.to_sexp, @right.to_sexp]
end
end
end
end
22 changes: 16 additions & 6 deletions lib/poison/bootstrap/syntax/value.rb
Expand Up @@ -12,22 +12,32 @@ def to_sexp
end
end

class Boolean < Value
class Literal < Node
def initialize(value)
@value = value
end

def to_sexp
[@value, nil, nil]
end
end

class Boolean < Literal
end

class Integer < Value
class Integer < Literal
end

class Real < Value
class Real < Literal
end

class Imaginary < Value
class Imaginary < Literal
end

class String < Value
class String < Literal
end

class NilKind < Value
class NilKind < Literal
def initialize
@value = nil
end
Expand Down
2 changes: 1 addition & 1 deletion spec/custom/matchers/parse_as.rb
Expand Up @@ -4,7 +4,7 @@ def initialize(expected)
end

def matches?(actual)
@actual = Poison::Parser.new.parse(actual).to_sexp.last
@actual = Poison::Parser.new.parse(actual).to_sexp
@actual == @expected
end

Expand Down

0 comments on commit c87d4f7

Please sign in to comment.