bracket_notation 1.0.3 → 1.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +5 -0
- data/bracket_notation.gemspec +3 -3
- data/init.rb +1 -1
- data/lib/bracket_notation/evaluator.rb +6 -3
- data/lib/bracket_notation/parser.rb +38 -40
- data/lib/bracket_notation/scanner.rb +10 -5
- data/lib/bracket_notation/version.rb +1 -1
- data/test/functional/evaluator_test.rb +1 -1
- data/test/functional/parser_test.rb +1 -1
- data/test/functional/scanner_test.rb +1 -1
- data/test/integration/parsing_test.rb +1 -1
- data/test/test_helper.rb +1 -2
- data/test/unit/expression_test.rb +1 -1
- data/test/unit/token_test.rb +1 -1
- metadata +3 -20
data/CHANGELOG
CHANGED
data/bracket_notation.gemspec
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{bracket_notation}
|
5
|
-
s.version = "1.0.
|
5
|
+
s.version = "1.0.4"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Cody Brimhall"]
|
9
|
-
s.date = %q{2011-
|
9
|
+
s.date = %q{2011-03-08}
|
10
10
|
s.description = %q{Generates a representation of a syntax tree using a string of bracket notation.}
|
11
11
|
s.email = %q{zbrimhall@gmail.com}
|
12
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"]
|
@@ -15,7 +15,7 @@ Gem::Specification.new do |s|
|
|
15
15
|
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Bracket_notation", "--main", "README.rdoc"]
|
16
16
|
s.require_paths = ["lib"]
|
17
17
|
s.rubyforge_project = %q{bracket_notation}
|
18
|
-
s.rubygems_version = %q{1.
|
18
|
+
s.rubygems_version = %q{1.6.1}
|
19
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
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
21
|
|
data/init.rb
CHANGED
@@ -69,9 +69,12 @@ module BracketNotation # :nodoc:
|
|
69
69
|
|
70
70
|
while((token = next_token).type != Token::RBRACKET)
|
71
71
|
expression = case token.type
|
72
|
-
when Token::NAME
|
73
|
-
|
74
|
-
|
72
|
+
when Token::NAME
|
73
|
+
Terminal.new(token.value)
|
74
|
+
when Token::LBRACKET
|
75
|
+
evaluate_phrase
|
76
|
+
else
|
77
|
+
unexpected_token_error
|
75
78
|
end
|
76
79
|
|
77
80
|
identifier.add_child(expression)
|
@@ -38,62 +38,60 @@ module BracketNotation # :nodoc:
|
|
38
38
|
# validation process.
|
39
39
|
class ValidationError < RuntimeError; end
|
40
40
|
|
41
|
-
#
|
42
|
-
#
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
validate
|
49
|
-
end
|
50
|
-
|
51
|
-
# Scans and evaluates the input string, returning an expression tree.
|
52
|
-
def parse
|
53
|
-
scanner = Scanner.new(@data)
|
54
|
-
evaluator = Evaluator.new(scanner.scan)
|
55
|
-
expression = evaluator.evaluate
|
56
|
-
end
|
57
|
-
|
58
|
-
private
|
59
|
-
|
60
|
-
# Normalizes the input string to make it easier to parse.
|
61
|
-
def scrub(str)
|
62
|
-
output = str.gsub(/\t/, "")
|
63
|
-
output.gsub!(/\s+/, " ")
|
64
|
-
output.gsub!(/\] \[/, "][")
|
65
|
-
output.gsub!(/ \[/, "[")
|
66
|
-
|
67
|
-
return output
|
68
|
-
end
|
69
|
-
|
70
|
-
# Checks to see if the input is valid, i.e. it has a length, no unnamed
|
71
|
-
# nodes, and the bracket-nesting is balanced.
|
72
|
-
def validate
|
73
|
-
validation_error("Input string can't be empty.") if @data.length < 1
|
74
|
-
validation_error("All opening brackets must have a label.") if /\[\s*\[/ =~ @data
|
41
|
+
# Performs basic validation of a string without executing the entire parse
|
42
|
+
# process. Returns true if validation is successful; raises an exception if
|
43
|
+
# not.
|
44
|
+
def self.validate(input)
|
45
|
+
validation_error("parser input cannot be nil") if input.nil?
|
46
|
+
validation_error("input string can't be empty") if input.length < 1
|
47
|
+
validation_error("all opening brackets must have a label") if /\[\s*\[/ =~ input
|
75
48
|
|
76
49
|
# Count the opening and closing brackets to make sure they're balanced
|
77
|
-
chars =
|
78
|
-
validation_error("
|
50
|
+
chars = input.gsub(/[^\[\]]/, "").split(//)
|
51
|
+
validation_error("opening and closing brackets must be balanced") if chars.length % 2 != 0
|
79
52
|
|
80
53
|
open_count, close_count = 0, 0
|
81
54
|
|
82
55
|
chars.each do |char|
|
83
56
|
case char
|
84
|
-
when '['
|
85
|
-
|
57
|
+
when '['
|
58
|
+
open_count += 1
|
59
|
+
when ']'
|
60
|
+
close_count += 1
|
86
61
|
end
|
87
62
|
|
88
63
|
break if open_count < close_count
|
89
64
|
end
|
90
65
|
|
91
|
-
validation_error("
|
66
|
+
validation_error("opening and closing brackets must be properly nested") if open_count != close_count
|
67
|
+
|
68
|
+
return true
|
92
69
|
end
|
93
70
|
|
71
|
+
# Saves the input string, as well as a copy of the input string that has
|
72
|
+
# been normalized and validated.
|
73
|
+
def initialize(input)
|
74
|
+
@input = input
|
75
|
+
validate
|
76
|
+
end
|
77
|
+
|
78
|
+
# Scans and evaluates the input string, returning an expression tree.
|
79
|
+
def parse
|
80
|
+
Evaluator.new(Scanner.new(@input).scan).evaluate
|
81
|
+
end
|
82
|
+
|
83
|
+
private
|
84
|
+
|
94
85
|
# Raises a validation exception with the given message
|
95
|
-
def validation_error(message)
|
86
|
+
def self.validation_error(message)
|
96
87
|
raise ValidationError, message
|
97
88
|
end
|
89
|
+
|
90
|
+
# Performs basic validation of the input string without executing the entire
|
91
|
+
# parse process. Returns true if validation is successful; raises an
|
92
|
+
# exception if not.
|
93
|
+
def validate
|
94
|
+
self.class.validate(@input)
|
95
|
+
end
|
98
96
|
end
|
99
97
|
end
|
@@ -76,11 +76,16 @@ module BracketNotation # :nodoc:
|
|
76
76
|
token = nil
|
77
77
|
while(token.nil?)
|
78
78
|
token = case read_char
|
79
|
-
when UNRESERVED_CHARACTER
|
80
|
-
|
81
|
-
when
|
82
|
-
|
83
|
-
|
79
|
+
when UNRESERVED_CHARACTER
|
80
|
+
name_token
|
81
|
+
when LBRACKET_CHARACTER
|
82
|
+
Token.LBRACKET
|
83
|
+
when RBRACKET_CHARACTER
|
84
|
+
Token.RBRACKET
|
85
|
+
when EOL_CHARACTER
|
86
|
+
Token.EOL
|
87
|
+
else
|
88
|
+
nil
|
84
89
|
end
|
85
90
|
end
|
86
91
|
|
@@ -26,7 +26,7 @@
|
|
26
26
|
# Copyright:: Copyright (c) 2010 Cody Brimhall
|
27
27
|
# License:: Distributed under the terms of the GNU General Public License, v. 3
|
28
28
|
|
29
|
-
require '
|
29
|
+
require 'test_helper'
|
30
30
|
|
31
31
|
class EvaluatorTest < Test::Unit::TestCase
|
32
32
|
include BracketNotation
|
@@ -26,7 +26,7 @@
|
|
26
26
|
# Copyright:: Copyright (c) 2010 Cody Brimhall
|
27
27
|
# License:: Distributed under the terms of the GNU General Public License, v. 3
|
28
28
|
|
29
|
-
require '
|
29
|
+
require 'test_helper'
|
30
30
|
|
31
31
|
class ParserTest < Test::Unit::TestCase
|
32
32
|
include BracketNotation
|
@@ -26,7 +26,7 @@
|
|
26
26
|
# Copyright:: Copyright (c) 2010 Cody Brimhall
|
27
27
|
# License:: Distributed under the terms of the GNU General Public License, v. 3
|
28
28
|
|
29
|
-
require '
|
29
|
+
require 'test_helper'
|
30
30
|
|
31
31
|
class ScannerTest < Test::Unit::TestCase
|
32
32
|
include BracketNotation
|
@@ -26,7 +26,7 @@
|
|
26
26
|
# Copyright:: Copyright (c) 2010 Cody Brimhall
|
27
27
|
# License:: Distributed under the terms of the GNU General Public License, v. 3
|
28
28
|
|
29
|
-
require '
|
29
|
+
require 'test_helper'
|
30
30
|
|
31
31
|
class ParsingTest < Test::Unit::TestCase
|
32
32
|
include BracketNotation
|
data/test/test_helper.rb
CHANGED
@@ -26,7 +26,7 @@
|
|
26
26
|
# Copyright:: Copyright (c) 2010 Cody Brimhall
|
27
27
|
# License:: Distributed under the terms of the GNU General Public License, v. 3
|
28
28
|
|
29
|
-
require '
|
29
|
+
require 'test_helper'
|
30
30
|
|
31
31
|
class ExpressionTest < Test::Unit::TestCase
|
32
32
|
include BracketNotation
|
data/test/unit/token_test.rb
CHANGED
@@ -26,7 +26,7 @@
|
|
26
26
|
# Copyright:: Copyright (c) 2010 Cody Brimhall
|
27
27
|
# License:: Distributed under the terms of the GNU General Public License, v. 3
|
28
28
|
|
29
|
-
require '
|
29
|
+
require 'test_helper'
|
30
30
|
|
31
31
|
class TokenTest < Test::Unit::TestCase
|
32
32
|
include BracketNotation
|
metadata
CHANGED
@@ -1,13 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bracket_notation
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash: 17
|
5
4
|
prerelease:
|
6
|
-
|
7
|
-
- 1
|
8
|
-
- 0
|
9
|
-
- 3
|
10
|
-
version: 1.0.3
|
5
|
+
version: 1.0.4
|
11
6
|
platform: ruby
|
12
7
|
authors:
|
13
8
|
- Cody Brimhall
|
@@ -15,7 +10,7 @@ autorequire:
|
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
12
|
|
18
|
-
date: 2011-
|
13
|
+
date: 2011-03-08 00:00:00 -08:00
|
19
14
|
default_executable:
|
20
15
|
dependencies:
|
21
16
|
- !ruby/object:Gem::Dependency
|
@@ -26,11 +21,6 @@ dependencies:
|
|
26
21
|
requirements:
|
27
22
|
- - ">="
|
28
23
|
- !ruby/object:Gem::Version
|
29
|
-
hash: 37
|
30
|
-
segments:
|
31
|
-
- 2
|
32
|
-
- 11
|
33
|
-
- 3
|
34
24
|
version: 2.11.3
|
35
25
|
type: :development
|
36
26
|
version_requirements: *id001
|
@@ -98,24 +88,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
98
88
|
requirements:
|
99
89
|
- - ">="
|
100
90
|
- !ruby/object:Gem::Version
|
101
|
-
hash: 3
|
102
|
-
segments:
|
103
|
-
- 0
|
104
91
|
version: "0"
|
105
92
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
93
|
none: false
|
107
94
|
requirements:
|
108
95
|
- - ">="
|
109
96
|
- !ruby/object:Gem::Version
|
110
|
-
hash: 11
|
111
|
-
segments:
|
112
|
-
- 1
|
113
|
-
- 2
|
114
97
|
version: "1.2"
|
115
98
|
requirements: []
|
116
99
|
|
117
100
|
rubyforge_project: bracket_notation
|
118
|
-
rubygems_version: 1.
|
101
|
+
rubygems_version: 1.6.1
|
119
102
|
signing_key:
|
120
103
|
specification_version: 3
|
121
104
|
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.
|