forked from nathansobo/treetop
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added possibility do define Treetop parsers in pure Ruby code.
Extended some core Ruby classes. Modified 'lib/treetop/ruby_extensions.rb' file to load all .rb files from 'lib/treetop/ruby_extensions' directory.
- Loading branch information
Showing
13 changed files
with
426 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ | |
*.ipr | ||
doc/site/*.* | ||
benchmark/*.dat | ||
benchmark/*.log | ||
benchmark/*.log | ||
*~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
dir = File.dirname(__FILE__) | ||
require File.expand_path("#{dir}/test_helper") | ||
|
||
class SyntaxTest < Test::Unit::TestCase | ||
include Treetop::Syntax | ||
include SyntaxTestHelper | ||
|
||
def test_simple | ||
assert_grammar { | ||
grammar :OnlyGrammar do | ||
end | ||
} | ||
end | ||
|
||
def test_rules | ||
assert_grammar { | ||
grammar :Simple do | ||
rule :foo do | ||
["foo", :bar] | ||
end | ||
|
||
rule :bar do | ||
"bar" / "baz" | ||
end | ||
end | ||
} | ||
parse('foobar') | ||
parse('foobaz') | ||
end | ||
|
||
def test_nested | ||
assert_grammar { | ||
grammar :Nested do | ||
rule :nested do | ||
["foo", "bar", "baz" / "bop"] | ||
end | ||
end | ||
} | ||
parse('foobarbaz') | ||
parse('foobarbop') | ||
end | ||
|
||
def test_operators | ||
assert_grammar { | ||
grammar :Kleene do | ||
rule :Kleene do | ||
"foo".kleene | ||
end | ||
end | ||
} | ||
parse("") | ||
parse("foo") | ||
parse("foofoo") | ||
|
||
assert_grammar { | ||
grammar :Plus do | ||
rule :Plus do | ||
"foo".plus | ||
end | ||
end | ||
} | ||
parse("foo") | ||
parse("foofoo") | ||
|
||
assert_grammar { | ||
grammar :Optional do | ||
rule :Optional do | ||
"foo".mark | ||
end | ||
end | ||
} | ||
parse("") | ||
parse("foo") | ||
end | ||
|
||
def test_inclusion | ||
assert_grammar { | ||
grammar :One do | ||
rule :a do | ||
"foo" | ||
end | ||
|
||
rule :b do | ||
"baz" | ||
end | ||
end | ||
} | ||
|
||
assert_grammar { | ||
grammar :Two do | ||
include :One | ||
rule :a do | ||
:super / "bar" / :c | ||
end | ||
|
||
rule :c do | ||
:b | ||
end | ||
end | ||
} | ||
parse("foo") | ||
parse("bar") | ||
parse("baz") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require 'test/unit' | ||
require 'rubygems' | ||
require 'treetop' | ||
|
||
dir = File.dirname(__FILE__) | ||
require File.expand_path("#{dir}/../../lib/treetop/ruby_extensions") | ||
require File.expand_path("#{dir}/../../lib/treetop/syntax") | ||
|
||
module SyntaxTestHelper | ||
def assert_grammar | ||
g = yield | ||
assert_not_nil g | ||
flunk "Badly generated parser" unless g | ||
@parser = eval("#{g}.new") | ||
end | ||
|
||
def parse(input) | ||
result = @parser.parse(input) | ||
unless result | ||
puts @parser.terminal_failures.join("\n") | ||
end | ||
assert_not_nil result | ||
if result | ||
assert_equal input, result.text_value | ||
end | ||
result | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
dir = File.dirname(__FILE__) | ||
require "#{dir}/ruby_extensions/string" | ||
Dir.glob("#{dir}/ruby_extensions/*.rb").each do |file| | ||
require file | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class Array | ||
def join_with(method, pattern = "") | ||
return join(pattern) unless method | ||
return "" if self.length == 0 | ||
|
||
args = [] | ||
if method.respond_to? :to_hash | ||
args = method[:args] || [] | ||
method = method[:name] | ||
end | ||
|
||
output = self[0].send(method, *args) | ||
for i in (1...self.length) | ||
output += pattern + self[i].send(method, *args) | ||
end | ||
output | ||
end | ||
|
||
def to_tt | ||
self.join_with({:name => :seq_to_tt, :args => [true]}, " ") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class NilClass | ||
def to_tt | ||
"" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
class Object | ||
def sequence | ||
@sequence ||= [] | ||
end | ||
|
||
def /(other) | ||
sequence.push(other) | ||
self | ||
end | ||
|
||
def seq_to_tt(inline = false) | ||
separator = inline ? " / " : "\n/\n" | ||
tt = if sequence.length == 0 | ||
self.to_tt | ||
else | ||
output = self.to_tt + separator + sequence.join_with({:name => :seq_to_tt, :args => [true]}, separator) | ||
output = "( #{output} )" if inline | ||
output | ||
end | ||
|
||
# Operators | ||
tt = "&" + tt if @amper | ||
tt = "!" + tt if @bang | ||
tt += "*" if @kleene | ||
tt += "+" if @plus | ||
tt += "?" if @mark | ||
|
||
tt += " <#{@node.to_s}>" if @node | ||
tt += " {\n#{@block.gsub("\t", " ").justify.indent_paragraph(2)}\n}" if @block | ||
tt = @label.to_s + ':' + tt if @label | ||
tt | ||
end | ||
|
||
def node(name) | ||
@node = name | ||
self | ||
end | ||
|
||
def block(content) | ||
@block = content | ||
self | ||
end | ||
|
||
def label(name) | ||
@label = name | ||
self | ||
end | ||
|
||
[:mark, :kleene, :plus, :amper, :bang].each do |sym| | ||
Object.class_eval(%{ | ||
def #{sym.to_s} | ||
@#{sym.to_s} = true | ||
self | ||
end | ||
}) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class Regexp | ||
def to_tt | ||
self.inspect | ||
end | ||
end |
Oops, something went wrong.