treetop 1.4.4 → 1.4.5

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,29 +1,25 @@
1
- dir = File.dirname(__FILE__)
2
1
  require 'rubygems'
3
2
  require 'rake'
4
- $LOAD_PATH.unshift(File.join(dir, 'vendor', 'rspec', 'lib'))
5
- require 'spec/rake/spectask'
6
-
7
3
  require 'rake/gempackagetask'
4
+ require 'spec/rake/spectask'
8
5
 
9
6
  task :default => :spec
10
-
11
7
  Spec::Rake::SpecTask.new do |t|
12
8
  t.pattern = 'spec/**/*spec.rb'
9
+ t.libs << 'spec'
13
10
  end
14
11
 
15
12
  load "./treetop.gemspec"
16
-
17
13
  Rake::GemPackageTask.new($gemspec) do |pkg|
18
14
  pkg.need_tar = true
19
15
  end
20
16
 
21
- task :spec => :regenerate_metagrammar
22
- task :regenerate_metagrammar => 'lib/treetop/compiler/metagrammar.treetop' do |t|
17
+ task :spec => 'lib/treetop/compiler/metagrammar.treetop'
18
+ file 'lib/treetop/compiler/metagrammar.treetop' do |t|
23
19
  unless $bootstrapped_gen_1_metagrammar
24
- load File.join(File.dirname(__FILE__), 'lib', 'treetop', 'bootstrap_gen_1_metagrammar.rb')
20
+ load File.expand_path('../lib/treetop/bootstrap_gen_1_metagrammar.rb', __FILE__)
25
21
  end
26
-
22
+
27
23
  Treetop::Compiler::GrammarCompiler.new.compile(METAGRAMMAR_PATH)
28
24
  end
29
25
 
data/bin/tt CHANGED
@@ -5,10 +5,11 @@ require 'rubygems'
5
5
  $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + "/../lib"))
6
6
  require 'treetop'
7
7
  require 'treetop/version'
8
+ require 'treetop/polyglot'
8
9
 
9
10
  options = {}
10
11
  parser = OptionParser.new do |opts|
11
- exts = Treetop::VALID_GRAMMAR_EXT.collect { |i| '.' + i }
12
+ exts = Treetop::Polyglot::VALID_GRAMMAR_EXT.collect { |i| '.' + i }
12
13
 
13
14
  opts.banner = "Treetop Parsing Expression Grammar (PEG) Comand Line Compiler"
14
15
  opts.define_head "Usage: tt [options] grammar_file[#{exts.join('|')}] ..."
@@ -57,7 +58,7 @@ end
57
58
 
58
59
  def grammar_exist?(filename)
59
60
  if File.extname(filename).empty?
60
- Treetop::VALID_GRAMMAR_EXT.each do |ext|
61
+ Treetop::Polyglot::VALID_GRAMMAR_EXT.each do |ext|
61
62
  fn_ext = "#{filename}.#{ext}"
62
63
  return true if File.exist?(fn_ext) && !File.zero?(fn_ext)
63
64
  end
@@ -67,7 +68,7 @@ end
67
68
 
68
69
  def full_grammar_filename(filename)
69
70
  return filename if !File.extname(filename).empty?
70
- Treetop::VALID_GRAMMAR_EXT.each do |ext|
71
+ Treetop::Polyglot::VALID_GRAMMAR_EXT.each do |ext|
71
72
  fn_ext = "#{filename}.#{ext}"
72
73
  return fn_ext if File.exist?(fn_ext) && !File.zero?(fn_ext)
73
74
  end
@@ -96,7 +97,7 @@ while !file_list.empty?
96
97
 
97
98
  # try to compile
98
99
  treetop_file = full_grammar_filename(treetop_file)
99
- std_output_file = treetop_file.gsub(Treetop::VALID_GRAMMAR_EXT_REGEXP, '.rb')
100
+ std_output_file = treetop_file.gsub(Treetop::Polyglot::VALID_GRAMMAR_EXT_REGEXP, '.rb')
100
101
 
101
102
  if options[:out_file]
102
103
  # explicit output file name option; never overwrite unless forced
@@ -8,6 +8,13 @@ You can `.treetop` files into Ruby source code with the `tt` command line script
8
8
  ##Loading A Grammar Directly
9
9
  The Polyglot gem makes it possible to load `.treetop` or `.tt` files directly with `require`. This will invoke `Treetop.load`, which automatically compiles the grammar to Ruby and then evaluates the Ruby source. If you are getting errors in methods you define on the syntax tree, try using the command line compiler for better stack trace feedback. A better solution to this issue is in the works.
10
10
 
11
+ In order to use Polyglot dynamic loading of `.treetop` or `.tt` files though, you need to require the Polyglot gem before you require the Treetop gem as Treetop will only create hooks into Polyglot for the treetop files if Polyglot is already loaded. So you need to use:
12
+
13
+ require 'polyglot'
14
+ require 'treetop'
15
+
16
+ in order to use Polyglot auto loading with Treetop in Ruby.
17
+
11
18
  ##Instantiating and Using Parsers
12
19
  If a grammar by the name of `Foo` is defined, the compiled Ruby source will define a `FooParser` class. To parse input, create an instance and call its `parse` method with a string. The parser will return the syntax tree of the match or `nil` if there is a failure.
13
20
 
data/lib/treetop.rb CHANGED
@@ -1,16 +1,3 @@
1
- require 'rubygems'
2
-
3
- module Treetop
4
- VALID_GRAMMAR_EXT = ['treetop', 'tt']
5
- VALID_GRAMMAR_EXT_REGEXP = /\.(#{VALID_GRAMMAR_EXT.join('|')})\Z/o
6
- end
7
-
8
- dir = File.dirname(__FILE__)
9
-
10
- TREETOP_ROOT = File.join(dir, 'treetop')
11
- require File.join(TREETOP_ROOT, "ruby_extensions")
12
- require File.join(TREETOP_ROOT, "runtime")
13
- require File.join(TREETOP_ROOT, "compiler")
14
-
15
- require 'polyglot'
16
- Polyglot.register(Treetop::VALID_GRAMMAR_EXT, Treetop)
1
+ require 'treetop/runtime'
2
+ require 'treetop/compiler'
3
+ require 'treetop/polyglot'
@@ -2,28 +2,25 @@
2
2
  # into the environment by compiling the current metagrammar.treetop using a trusted version of Treetop.
3
3
 
4
4
  require 'rubygems'
5
- dir = File.dirname(__FILE__)
6
5
 
7
6
  TREETOP_VERSION_REQUIRED_TO_BOOTSTRAP = '>= 1.1.5'
8
7
 
9
8
  # Loading trusted version of Treetop to compile the compiler
10
9
  gem_spec = Gem.source_index.find_name('treetop', TREETOP_VERSION_REQUIRED_TO_BOOTSTRAP).last
11
10
  raise "Install a Treetop Gem version #{TREETOP_VERSION_REQUIRED_TO_BOOTSTRAP} to bootstrap." unless gem_spec
12
- trusted_treetop_path = gem_spec.full_gem_path
13
- require File.join(trusted_treetop_path, 'lib', 'treetop')
11
+ require "#{gem_spec.full_gem_path}/lib/treetop"
14
12
 
15
13
  # Relocating trusted version of Treetop to Trusted::Treetop
16
14
  Trusted = Module.new
17
15
  Trusted::Treetop = Treetop
18
16
  Object.send(:remove_const, :Treetop)
19
- Object.send(:remove_const, :TREETOP_ROOT)
20
17
 
21
18
  # Requiring version of Treetop that is under test
22
19
  $exclude_metagrammar = true
23
- require File.expand_path(File.join(dir, '..', 'treetop'))
20
+ require File.expand_path('../treetop')
24
21
 
25
22
  # Compile and evaluate freshly generated metagrammar source
26
- METAGRAMMAR_PATH = File.join(TREETOP_ROOT, 'compiler', 'metagrammar.treetop')
23
+ METAGRAMMAR_PATH = File.expand_path('../compiler/metagrammar.treetop', __FILE__)
27
24
  compiled_metagrammar_source = Trusted::Treetop::Compiler::GrammarCompiler.new.ruby_source(METAGRAMMAR_PATH)
28
25
  Object.class_eval(compiled_metagrammar_source)
29
26
 
@@ -42,4 +39,4 @@ Object.class_eval(compiled_metagrammar_source)
42
39
  # include Trusted::Treetop::Runtime
43
40
  # end
44
41
 
45
- $bootstrapped_gen_1_metagrammar = true
42
+ $bootstrapped_gen_1_metagrammar = true
@@ -1,6 +1,7 @@
1
- dir = File.dirname(__FILE__)
2
- require File.join(dir, *%w[compiler lexical_address_space])
3
- require File.join(dir, *%w[compiler ruby_builder])
4
- require File.join(dir, *%w[compiler node_classes])
5
- require File.join(dir, *%w[compiler metagrammar]) unless defined?($exclude_metagrammar)
6
- require File.join(dir, *%w[compiler grammar_compiler])
1
+ require 'treetop/ruby_extensions'
2
+
3
+ require 'treetop/compiler/lexical_address_space'
4
+ require 'treetop/compiler/ruby_builder'
5
+ require 'treetop/compiler/node_classes'
6
+ require 'treetop/compiler/metagrammar' unless defined?($exclude_metagrammar)
7
+ require 'treetop/compiler/grammar_compiler'
@@ -7,7 +7,7 @@ module Treetop
7
7
  include Treetop::Runtime
8
8
 
9
9
  def root
10
- @root || :treetop_file
10
+ @root ||= :treetop_file
11
11
  end
12
12
 
13
13
  module TreetopFile0
@@ -1,20 +1,19 @@
1
- dir = File.dirname(__FILE__)
2
- require File.join(dir, *%w[node_classes parsing_expression])
3
- require File.join(dir, *%w[node_classes atomic_expression])
4
- require File.join(dir, *%w[node_classes inline_module])
5
- require File.join(dir, *%w[node_classes predicate_block])
6
- require File.join(dir, *%w[node_classes treetop_file])
7
- require File.join(dir, *%w[node_classes grammar])
8
- require File.join(dir, *%w[node_classes declaration_sequence])
9
- require File.join(dir, *%w[node_classes parsing_rule])
10
- require File.join(dir, *%w[node_classes parenthesized_expression])
11
- require File.join(dir, *%w[node_classes nonterminal])
12
- require File.join(dir, *%w[node_classes terminal])
13
- require File.join(dir, *%w[node_classes anything_symbol])
14
- require File.join(dir, *%w[node_classes character_class])
15
- require File.join(dir, *%w[node_classes sequence])
16
- require File.join(dir, *%w[node_classes choice])
17
- require File.join(dir, *%w[node_classes repetition])
18
- require File.join(dir, *%w[node_classes optional])
19
- require File.join(dir, *%w[node_classes predicate])
20
- require File.join(dir, *%w[node_classes transient_prefix])
1
+ require 'treetop/compiler/node_classes/parsing_expression'
2
+ require 'treetop/compiler/node_classes/atomic_expression'
3
+ require 'treetop/compiler/node_classes/inline_module'
4
+ require 'treetop/compiler/node_classes/predicate_block'
5
+ require 'treetop/compiler/node_classes/treetop_file'
6
+ require 'treetop/compiler/node_classes/grammar'
7
+ require 'treetop/compiler/node_classes/declaration_sequence'
8
+ require 'treetop/compiler/node_classes/parsing_rule'
9
+ require 'treetop/compiler/node_classes/parenthesized_expression'
10
+ require 'treetop/compiler/node_classes/nonterminal'
11
+ require 'treetop/compiler/node_classes/terminal'
12
+ require 'treetop/compiler/node_classes/anything_symbol'
13
+ require 'treetop/compiler/node_classes/character_class'
14
+ require 'treetop/compiler/node_classes/sequence'
15
+ require 'treetop/compiler/node_classes/choice'
16
+ require 'treetop/compiler/node_classes/repetition'
17
+ require 'treetop/compiler/node_classes/optional'
18
+ require 'treetop/compiler/node_classes/predicate'
19
+ require 'treetop/compiler/node_classes/transient_prefix'
@@ -5,7 +5,7 @@ module Treetop
5
5
  def compile(builder)
6
6
  unless rules.empty?
7
7
  builder.method_declaration("root") do
8
- builder << "@root || :#{rules.first.name}"
8
+ builder << "@root ||= :#{rules.first.name}"
9
9
  end
10
10
  builder.newline
11
11
  end
@@ -1,3 +1,5 @@
1
+ require 'treetop/ruby_extensions/string'
2
+
1
3
  module Treetop
2
4
  module Compiler
3
5
  class RubyBuilder
@@ -0,0 +1,9 @@
1
+ module Treetop
2
+ module Polyglot
3
+ VALID_GRAMMAR_EXT = ['treetop', 'tt']
4
+ VALID_GRAMMAR_EXT_REGEXP = /\.(#{VALID_GRAMMAR_EXT.join('|')})\Z/o
5
+ end
6
+ end
7
+
8
+ require 'polyglot'
9
+ Polyglot.register(Treetop::Polyglot::VALID_GRAMMAR_EXT, Treetop)
@@ -1,2 +1 @@
1
- dir = File.dirname(__FILE__)
2
- require "#{dir}/ruby_extensions/string"
1
+ require 'treetop/ruby_extensions/string'
@@ -1,5 +1,6 @@
1
- dir = File.dirname(__FILE__)
2
- require "#{dir}/runtime/compiled_parser"
3
- require "#{dir}/runtime/syntax_node"
4
- require "#{dir}/runtime/terminal_parse_failure"
5
- require "#{dir}/runtime/interval_skip_list"
1
+ require 'treetop/ruby_extensions'
2
+
3
+ require 'treetop/runtime/compiled_parser'
4
+ require 'treetop/runtime/syntax_node'
5
+ require 'treetop/runtime/terminal_parse_failure'
6
+ require 'treetop/runtime/interval_skip_list'
@@ -2,12 +2,12 @@ module Treetop
2
2
  module Runtime
3
3
  class CompiledParser
4
4
  include Treetop::Runtime
5
-
5
+
6
6
  attr_reader :input, :index, :max_terminal_failure_index
7
7
  attr_writer :root
8
8
  attr_accessor :consume_all_input
9
9
  alias :consume_all_input? :consume_all_input
10
-
10
+
11
11
  def initialize
12
12
  self.consume_all_input = true
13
13
  end
@@ -43,17 +43,21 @@ module Treetop
43
43
  " at line #{failure_line}, column #{failure_column} (byte #{failure_index+1})" +
44
44
  " after #{input[index...failure_index]}"
45
45
  end
46
-
46
+
47
47
  def terminal_failures
48
- @terminal_failures.map! {|tf_ary| TerminalParseFailure.new(*tf_ary) }
48
+ if @terminal_failures.empty? || @terminal_failures[0].is_a?(TerminalParseFailure)
49
+ @terminal_failures
50
+ else
51
+ @terminal_failures.map! {|tf_ary| TerminalParseFailure.new(*tf_ary) }
52
+ end
49
53
  end
50
54
 
51
55
 
52
56
  protected
53
-
57
+
54
58
  attr_reader :node_cache, :input_length
55
59
  attr_writer :index
56
-
60
+
57
61
  def prepare_to_parse(input)
58
62
  @input = input
59
63
  @input_length = input.length
@@ -63,11 +67,11 @@ module Treetop
63
67
  @terminal_failures = []
64
68
  @max_terminal_failure_index = 0
65
69
  end
66
-
70
+
67
71
  def reset_index
68
72
  @index = 0
69
73
  end
70
-
74
+
71
75
  def parse_anything(node_class = SyntaxNode, inline_module = nil)
72
76
  if index < input.length
73
77
  result = instantiate_node(node_class,input, index...(index + 1))
@@ -78,15 +82,15 @@ module Treetop
78
82
  terminal_parse_failure("any character")
79
83
  end
80
84
  end
81
-
85
+
82
86
  def instantiate_node(node_type,*args)
83
- if node_type.respond_to? :new
87
+ if node_type.respond_to? :new
84
88
  node_type.new(*args)
85
89
  else
86
90
  SyntaxNode.new(*args).extend(node_type)
87
91
  end
88
92
  end
89
-
93
+
90
94
  def has_terminal?(terminal, regex, index)
91
95
  if regex
92
96
  rx = @regexps[terminal] ||= Regexp.new(terminal)
@@ -95,7 +99,7 @@ module Treetop
95
99
  input[index, terminal.size] == terminal
96
100
  end
97
101
  end
98
-
102
+
99
103
  def terminal_parse_failure(expected_string)
100
104
  return nil if index < max_terminal_failure_index
101
105
  if index > max_terminal_failure_index
@@ -1,4 +1,3 @@
1
- dir = File.dirname(__FILE__)
2
- require "#{dir}/interval_skip_list/interval_skip_list.rb"
3
- require "#{dir}/interval_skip_list/head_node.rb"
4
- require "#{dir}/interval_skip_list/node.rb"
1
+ require 'treetop/runtime/interval_skip_list/interval_skip_list'
2
+ require 'treetop/runtime/interval_skip_list/head_node'
3
+ require 'treetop/runtime/interval_skip_list/node'
@@ -2,7 +2,7 @@ module Treetop #:nodoc:
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 1
4
4
  MINOR = 4
5
- TINY = 4
5
+ TINY = 5
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: treetop
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.4
4
+ version: 1.4.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Sobo
@@ -9,7 +9,7 @@ autorequire: treetop
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-20 00:00:00 +11:00
12
+ date: 2010-03-29 00:00:00 +11:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -20,7 +20,7 @@ dependencies:
20
20
  requirements:
21
21
  - - ">="
22
22
  - !ruby/object:Gem::Version
23
- version: 0.3.0
23
+ version: 0.3.1
24
24
  version:
25
25
  description:
26
26
  email: nathansobo@gmail.com
@@ -61,6 +61,7 @@ files:
61
61
  - lib/treetop/compiler/node_classes.rb
62
62
  - lib/treetop/compiler/ruby_builder.rb
63
63
  - lib/treetop/compiler.rb
64
+ - lib/treetop/polyglot.rb
64
65
  - lib/treetop/ruby_extensions/string.rb
65
66
  - lib/treetop/ruby_extensions.rb
66
67
  - lib/treetop/runtime/compiled_parser.rb