bracket_notation 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,23 @@
1
+ CHANGELOG
2
+ COPYING
3
+ README.rdoc
4
+ Rakefile
5
+ init.rb
6
+ lib/bracket_notation.rb
7
+ lib/bracket_notation/evaluator.rb
8
+ lib/bracket_notation/expressions.rb
9
+ lib/bracket_notation/expressions/expression.rb
10
+ lib/bracket_notation/expressions/identifier.rb
11
+ lib/bracket_notation/expressions/terminal.rb
12
+ lib/bracket_notation/parser.rb
13
+ lib/bracket_notation/scanner.rb
14
+ lib/bracket_notation/token.rb
15
+ lib/bracket_notation/version.rb
16
+ test/functional/evaluator_test.rb
17
+ test/functional/parser_test.rb
18
+ test/functional/scanner_test.rb
19
+ test/integration/parsing_test.rb
20
+ test/test_helper.rb
21
+ test/unit/expression_test.rb
22
+ test/unit/token_test.rb
23
+ Manifest
@@ -0,0 +1,11 @@
1
+ BracketNotation is a parser for generating syntax trees from sentences
2
+ annotated with the kind of bracket notation that is commonly used by
3
+ linguists. The result is a tree structure with nodes that describe the phrases
4
+ and constituents of the sentence.
5
+
6
+ BracketNotation was inspired by Yoichiro Hasebe's RSyntaxTree[http://yohasebe.com/rsyntaxtree/],
7
+ and small portions of his code have been incorporated in the parser.
8
+
9
+ Author:: Cody Brimhall (mailto:brimhall@somuchwit.com)
10
+ Copyright:: Copyright (c) 2010 Cody Brimhall
11
+ License:: Distributed under the terms of the GNU General Public License, v. 3
@@ -0,0 +1,42 @@
1
+ #--
2
+ # This file is part of BracketNotation.
3
+ #
4
+ # BracketNotation is free software: you can redistribute it and/or modify
5
+ # it under the terms of the GNU General Public License as published by
6
+ # the Free Software Foundation, either version 3 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # BracketNotation is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with BracketNotation. If not, see <http://www.gnu.org/licenses/>.
16
+ #++
17
+ # BracketNotation is a parser for generating syntax trees from sentences
18
+ # annotated with the kind of bracket notation that is commonly used by
19
+ # linguists. The result is a tree structure with nodes that describe the phrases
20
+ # and constituents of the sentence.
21
+ #
22
+ # BracketNotation was inspired by Yoichiro Hasebe's RSyntaxTree[http://yohasebe.com/rsyntaxtree/],
23
+ # and small portions of his code have been incorporated in the parser.
24
+ #
25
+ # Author:: Cody Brimhall (mailto:brimhall@somuchwit.com)
26
+ # Copyright:: Copyright (c) 2010 Cody Brimhall
27
+ # License:: Distributed under the terms of the GNU General Public License, v. 3
28
+
29
+ ($:.unshift File.expand_path(File.join( File.dirname(__FILE__), 'lib' ))).uniq!
30
+
31
+ require 'echoe'
32
+ require 'bracket_notation/version'
33
+
34
+ Echoe.new('bracket_notation', BracketNotation::Version) do |p|
35
+ p.description = "Generates a representation of a syntax tree using a string of bracket notation."
36
+ p.summary = "Provides a parser for strings that have been marked up with the bracket notation commonly used by syntacticians. The parser generates an abstract tree representation of the syntax of the string."
37
+ p.url = "http://github.com/zbrimhall/bracket_notation"
38
+ p.author = "Cody Brimhall"
39
+ p.email = "zbrimhall@gmail.com"
40
+ p.ignore_pattern = %w(tmp/* script/* *.bbprojectd/*)
41
+ p.development_dependencies = ['shoulda >=2.11.3']
42
+ end
@@ -0,0 +1,33 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{bracket_notation}
5
+ s.version = "1.0.3"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Cody Brimhall"]
9
+ s.date = %q{2011-01-08}
10
+ s.description = %q{Generates a representation of a syntax tree using a string of bracket notation.}
11
+ s.email = %q{zbrimhall@gmail.com}
12
+ s.extra_rdoc_files = ["CHANGELOG", "COPYING", "README.rdoc", "lib/bracket_notation.rb", "lib/bracket_notation/evaluator.rb", "lib/bracket_notation/expressions.rb", "lib/bracket_notation/expressions/expression.rb", "lib/bracket_notation/expressions/identifier.rb", "lib/bracket_notation/expressions/terminal.rb", "lib/bracket_notation/parser.rb", "lib/bracket_notation/scanner.rb", "lib/bracket_notation/token.rb", "lib/bracket_notation/version.rb"]
13
+ s.files = ["CHANGELOG", "COPYING", "README.rdoc", "Rakefile", "init.rb", "lib/bracket_notation.rb", "lib/bracket_notation/evaluator.rb", "lib/bracket_notation/expressions.rb", "lib/bracket_notation/expressions/expression.rb", "lib/bracket_notation/expressions/identifier.rb", "lib/bracket_notation/expressions/terminal.rb", "lib/bracket_notation/parser.rb", "lib/bracket_notation/scanner.rb", "lib/bracket_notation/token.rb", "lib/bracket_notation/version.rb", "test/functional/evaluator_test.rb", "test/functional/parser_test.rb", "test/functional/scanner_test.rb", "test/integration/parsing_test.rb", "test/test_helper.rb", "test/unit/expression_test.rb", "test/unit/token_test.rb", "Manifest", "bracket_notation.gemspec"]
14
+ s.homepage = %q{http://github.com/zbrimhall/bracket_notation}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Bracket_notation", "--main", "README.rdoc"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{bracket_notation}
18
+ s.rubygems_version = %q{1.4.2}
19
+ s.summary = %q{Provides a parser for strings that have been marked up with the bracket notation commonly used by syntacticians. The parser generates an abstract tree representation of the syntax of the string.}
20
+ s.test_files = ["test/functional/evaluator_test.rb", "test/functional/parser_test.rb", "test/functional/scanner_test.rb", "test/integration/parsing_test.rb", "test/test_helper.rb", "test/unit/expression_test.rb", "test/unit/token_test.rb"]
21
+
22
+ if s.respond_to? :specification_version then
23
+ s.specification_version = 3
24
+
25
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
26
+ s.add_development_dependency(%q<shoulda>, [">= 2.11.3"])
27
+ else
28
+ s.add_dependency(%q<shoulda>, [">= 2.11.3"])
29
+ end
30
+ else
31
+ s.add_dependency(%q<shoulda>, [">= 2.11.3"])
32
+ end
33
+ end
data/init.rb ADDED
@@ -0,0 +1,29 @@
1
+ #--
2
+ # This file is part of BracketNotation.
3
+ #
4
+ # BracketNotation is free software: you can redistribute it and/or modify
5
+ # it under the terms of the GNU General Public License as published by
6
+ # the Free Software Foundation, either version 3 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # BracketNotation is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with BracketNotation. If not, see <http://www.gnu.org/licenses/>.
16
+ #++
17
+ # BracketNotation is a parser for generating syntax trees from sentences
18
+ # annotated with the kind of bracket notation that is commonly used by
19
+ # linguists. The result is a tree structure with nodes that describe the phrases
20
+ # and constituents of the sentence.
21
+ #
22
+ # BracketNotation was inspired by Yoichiro Hasebe's RSyntaxTree[http://yohasebe.com/rsyntaxtree/],
23
+ # and small portions of his code have been incorporated in the parser.
24
+ #
25
+ # Author:: Cody Brimhall (mailto:brimhall@somuchwit.com)
26
+ # Copyright:: Copyright (c) 2010 Cody Brimhall
27
+ # License:: Distributed under the terms of the GNU General Public License, v. 3
28
+
29
+ require 'lib/bracket_notation'
@@ -0,0 +1,34 @@
1
+ #--
2
+ # This file is part of BracketNotation.
3
+ #
4
+ # BracketNotation is free software: you can redistribute it and/or modify
5
+ # it under the terms of the GNU General Public License as published by
6
+ # the Free Software Foundation, either version 3 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # BracketNotation is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with BracketNotation. If not, see <http://www.gnu.org/licenses/>.
16
+ #++
17
+ # BracketNotation is a parser for generating syntax trees from sentences
18
+ # annotated with the kind of bracket notation that is commonly used by
19
+ # linguists. The result is a tree structure with nodes that describe the phrases
20
+ # and constituents of the sentence.
21
+ #
22
+ # BracketNotation was inspired by Yoichiro Hasebe's RSyntaxTree[http://yohasebe.com/rsyntaxtree/],
23
+ # and small portions of his code have been incorporated in the parser.
24
+ #
25
+ # Author:: Cody Brimhall (mailto:brimhall@somuchwit.com)
26
+ # Copyright:: Copyright (c) 2010 Cody Brimhall
27
+ # License:: Distributed under the terms of the GNU General Public License, v. 3
28
+
29
+ require 'bracket_notation/evaluator'
30
+ require 'bracket_notation/expressions'
31
+ require 'bracket_notation/parser'
32
+ require 'bracket_notation/scanner'
33
+ require 'bracket_notation/token'
34
+ require 'bracket_notation/version'
@@ -0,0 +1,99 @@
1
+ #--
2
+ # This file is part of BracketNotation.
3
+ #
4
+ # BracketNotation is free software: you can redistribute it and/or modify
5
+ # it under the terms of the GNU General Public License as published by
6
+ # the Free Software Foundation, either version 3 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # BracketNotation is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with BracketNotation. If not, see <http://www.gnu.org/licenses/>.
16
+ #++
17
+ # BracketNotation is a parser for generating syntax trees from sentences
18
+ # annotated with the kind of bracket notation that is commonly used by
19
+ # linguists. The result is a tree structure with nodes that describe the phrases
20
+ # and constituents of the sentence.
21
+ #
22
+ # BracketNotation was inspired by Yoichiro Hasebe's RSyntaxTree[http://yohasebe.com/rsyntaxtree/],
23
+ # and small portions of his code have been incorporated in the parser.
24
+ #
25
+ # Author:: Cody Brimhall (mailto:brimhall@somuchwit.com)
26
+ # Copyright:: Copyright (c) 2010 Cody Brimhall
27
+ # License:: Distributed under the terms of the GNU General Public License, v. 3
28
+
29
+ module BracketNotation # :nodoc:
30
+ # This class takes a list of Token instances and evaluates them. The result is
31
+ # an abstract tree representation of the syntax of the original string fed to
32
+ # the parser.
33
+ class Evaluator
34
+ attr_reader :input
35
+
36
+ # This class is an Exception subclass that reports errors in the evaluation
37
+ # process.
38
+ class EvaluationError < SyntaxError; end
39
+
40
+ # Saves the token list for evaluation.
41
+ def initialize(input)
42
+ @input = input
43
+ @token_index = -1
44
+ @root = nil
45
+ end
46
+
47
+ # Evaluates a bracketed phrase and returns the Evaluation instance that
48
+ # represents the phrase.
49
+ def evaluate
50
+ return @root unless @root.nil?
51
+
52
+ token = next_token
53
+ unexpected_token_error if token.type != Token::LBRACKET
54
+ @root = evaluate_phrase
55
+
56
+ unexpected_token_error if next_token.type != Token::EOL
57
+
58
+ return @root
59
+ end
60
+
61
+ private
62
+
63
+ # Evaluates a phrase expression.
64
+ def evaluate_phrase
65
+ token = next_token
66
+ unexpected_token_error if token.type != Token::NAME
67
+
68
+ identifier = Identifier.new(token.value)
69
+
70
+ while((token = next_token).type != Token::RBRACKET)
71
+ expression = case token.type
72
+ when Token::NAME: Terminal.new(token.value)
73
+ when Token::LBRACKET: evaluate_phrase
74
+ else unexpected_token_error
75
+ end
76
+
77
+ identifier.add_child(expression)
78
+ end
79
+
80
+ return identifier
81
+ end
82
+
83
+ # Returns the token currently being evaluated.
84
+ def current_token
85
+ return @input[@token_index]
86
+ end
87
+
88
+ # Advances the token index and returns the new current token.
89
+ def next_token
90
+ @token_index += 1
91
+ return current_token
92
+ end
93
+
94
+ # Raises an unexpected token error.
95
+ def unexpected_token_error
96
+ raise EvaluationError, "Unexpected token: #{current_token.inspect}"
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,31 @@
1
+ #--
2
+ # This file is part of BracketNotation.
3
+ #
4
+ # BracketNotation is free software: you can redistribute it and/or modify
5
+ # it under the terms of the GNU General Public License as published by
6
+ # the Free Software Foundation, either version 3 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # BracketNotation is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with BracketNotation. If not, see <http://www.gnu.org/licenses/>.
16
+ #++
17
+ # BracketNotation is a parser for generating syntax trees from sentences
18
+ # annotated with the kind of bracket notation that is commonly used by
19
+ # linguists. The result is a tree structure with nodes that describe the phrases
20
+ # and constituents of the sentence.
21
+ #
22
+ # BracketNotation was inspired by Yoichiro Hasebe's RSyntaxTree[http://yohasebe.com/rsyntaxtree/],
23
+ # and small portions of his code have been incorporated in the parser.
24
+ #
25
+ # Author:: Cody Brimhall (mailto:brimhall@somuchwit.com)
26
+ # Copyright:: Copyright (c) 2010 Cody Brimhall
27
+ # License:: Distributed under the terms of the GNU General Public License, v. 3
28
+
29
+ require 'bracket_notation/expressions/expression'
30
+ require 'bracket_notation/expressions/identifier'
31
+ require 'bracket_notation/expressions/terminal'
@@ -0,0 +1,49 @@
1
+ #--
2
+ # This file is part of BracketNotation.
3
+ #
4
+ # BracketNotation is free software: you can redistribute it and/or modify
5
+ # it under the terms of the GNU General Public License as published by
6
+ # the Free Software Foundation, either version 3 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # BracketNotation is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with BracketNotation. If not, see <http://www.gnu.org/licenses/>.
16
+ #++
17
+ # BracketNotation is a parser for generating syntax trees from sentences
18
+ # annotated with the kind of bracket notation that is commonly used by
19
+ # linguists. The result is a tree structure with nodes that describe the phrases
20
+ # and constituents of the sentence.
21
+ #
22
+ # BracketNotation was inspired by Yoichiro Hasebe's RSyntaxTree[http://yohasebe.com/rsyntaxtree/],
23
+ # and small portions of his code have been incorporated in the parser.
24
+ #
25
+ # Author:: Cody Brimhall (mailto:brimhall@somuchwit.com)
26
+ # Copyright:: Copyright (c) 2010 Cody Brimhall
27
+ # License:: Distributed under the terms of the GNU General Public License, v. 3
28
+
29
+ module BracketNotation # :nodoc:
30
+ # This class represents the result of evaluating a token. It is an abstract
31
+ # class that should have a subclass for every possible kind of expression.
32
+ class Expression
33
+ attr_accessor :value, :depth, :parent
34
+
35
+ # Saves the token value and optional tree depth.
36
+ def initialize(value, depth = 0)
37
+ @value = value
38
+ @depth = depth
39
+ @parent = nil
40
+ end
41
+
42
+ # Prints a visual representation of the syntax tree.
43
+ def pretty_print
44
+ out = ""
45
+ @depth.times {out << "--"}
46
+ out << " #{@value}\n"
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,62 @@
1
+ #--
2
+ # This file is part of BracketNotation.
3
+ #
4
+ # BracketNotation is free software: you can redistribute it and/or modify
5
+ # it under the terms of the GNU General Public License as published by
6
+ # the Free Software Foundation, either version 3 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # BracketNotation is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with BracketNotation. If not, see <http://www.gnu.org/licenses/>.
16
+ #++
17
+ # BracketNotation is a parser for generating syntax trees from sentences
18
+ # annotated with the kind of bracket notation that is commonly used by
19
+ # linguists. The result is a tree structure with nodes that describe the phrases
20
+ # and constituents of the sentence.
21
+ #
22
+ # BracketNotation was inspired by Yoichiro Hasebe's RSyntaxTree[http://yohasebe.com/rsyntaxtree/],
23
+ # and small portions of his code have been incorporated in the parser.
24
+ #
25
+ # Author:: Cody Brimhall (mailto:brimhall@somuchwit.com)
26
+ # Copyright:: Copyright (c) 2010 Cody Brimhall
27
+ # License:: Distributed under the terms of the GNU General Public License, v. 3
28
+
29
+ module BracketNotation # :nodoc:
30
+ # This class represents an identifier expression. Identifers are expressions
31
+ # that can have children -- branch nodes, in other words.
32
+ class Identifier < Expression
33
+ attr_accessor :children, :depth
34
+
35
+ # Saves the identifier value and optional tree depth.
36
+ def initialize(value, depth = 0)
37
+ super
38
+ @children = []
39
+ end
40
+
41
+ # Adds the given expression to the children array and updates the new
42
+ # child's parent and depth attributes.
43
+ def add_child(expression)
44
+ expression.parent = self
45
+ expression.depth = @depth + 1
46
+ @children << expression
47
+ end
48
+
49
+ # Sets the new depth, and the new depth of all children.
50
+ def depth=(rvalue)
51
+ @depth = rvalue
52
+ children.each {|child| child.depth = rvalue + 1}
53
+ end
54
+
55
+ # Prints a visual representation of the syntax tree.
56
+ def pretty_print
57
+ out = super
58
+ @children.each {|child| out << child.pretty_print}
59
+ out
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,34 @@
1
+ #--
2
+ # This file is part of BracketNotation.
3
+ #
4
+ # BracketNotation is free software: you can redistribute it and/or modify
5
+ # it under the terms of the GNU General Public License as published by
6
+ # the Free Software Foundation, either version 3 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # BracketNotation is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with BracketNotation. If not, see <http://www.gnu.org/licenses/>.
16
+ #++
17
+ # BracketNotation is a parser for generating syntax trees from sentences
18
+ # annotated with the kind of bracket notation that is commonly used by
19
+ # linguists. The result is a tree structure with nodes that describe the phrases
20
+ # and constituents of the sentence.
21
+ #
22
+ # BracketNotation was inspired by Yoichiro Hasebe's RSyntaxTree[http://yohasebe.com/rsyntaxtree/],
23
+ # and small portions of his code have been incorporated in the parser.
24
+ #
25
+ # Author:: Cody Brimhall (mailto:brimhall@somuchwit.com)
26
+ # Copyright:: Copyright (c) 2010 Cody Brimhall
27
+ # License:: Distributed under the terms of the GNU General Public License, v. 3
28
+
29
+ module BracketNotation # :nodoc:
30
+ # This class represents a terminal expression. Terminals are expressions that
31
+ # do not have children -- leaf nodes, in other words.
32
+ class Terminal < Expression
33
+ end
34
+ end