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 CHANGED
@@ -1,5 +1,10 @@
1
1
  = Version History
2
2
 
3
+ == 1.0.4 / 2011-01-09:
4
+
5
+ * Added a validation class method to the Parser class to help with error
6
+ checking in applications
7
+
3
8
  == 1.0.3 / 2011-01-08:
4
9
 
5
10
  * Trying to fix version detection when building gem
@@ -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.3"
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-01-08}
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.4.2}
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
@@ -26,4 +26,4 @@
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 'lib/bracket_notation'
29
+ require './lib/bracket_notation'
@@ -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: Terminal.new(token.value)
73
- when Token::LBRACKET: evaluate_phrase
74
- else unexpected_token_error
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
- # Saves the input string, as well as a copy of the input string that has
42
- # been normalized and validated.
43
- def initialize(input)
44
- validation_error "Parser input cannot be nil" if input.nil?
45
-
46
- @input = input
47
- @data = scrub(input)
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 = @data.gsub(/[^\[\]]/, "").split(//)
78
- validation_error("Opening and closing brackets must be balanced.") if chars.length % 2 != 0
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 '[': open_count += 1
85
- when ']': close_count += 1
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("Opening and closing brackets must be properly nested.") if open_count != close_count
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: name_token
80
- when LBRACKET_CHARACTER: Token.LBRACKET
81
- when RBRACKET_CHARACTER: Token.RBRACKET
82
- when EOL_CHARACTER: Token.EOL
83
- else nil
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
 
@@ -30,7 +30,7 @@ module BracketNotation # :nodoc:
30
30
  module Version # :nodoc:
31
31
  MAJOR = 1
32
32
  MINOR = 0
33
- MAINT = 3
33
+ MAINT = 4
34
34
 
35
35
  # Returns the current version string.
36
36
  def self.to_s;
@@ -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 'test/test_helper'
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 'test/test_helper'
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 'test/test_helper'
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 'test/test_helper'
29
+ require 'test_helper'
30
30
 
31
31
  class ParsingTest < Test::Unit::TestCase
32
32
  include BracketNotation
@@ -27,6 +27,5 @@
27
27
  # License:: Distributed under the terms of the GNU General Public License, v. 3
28
28
 
29
29
  require 'test/unit'
30
- require 'rubygems'
31
30
  require 'shoulda'
32
- require 'lib/bracket_notation'
31
+ require 'bracket_notation'
@@ -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 'test/test_helper'
29
+ require 'test_helper'
30
30
 
31
31
  class ExpressionTest < 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 'test/test_helper'
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
- segments:
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-01-08 00:00:00 -08:00
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.4.2
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.