Class: TextPeg2RubyPeg

Inherits:
Object
  • Object
show all
Defined in:
lib/textpeg2rubypeg.rb

Constant Summary collapse

RESERVED_WORDS =

:nodoc:

%w{index text_to_parse cache sequences parse ignore any_character optional one_or_more any_number_of sequence followed_by not_followed_by uncached_terminal uncached_terminal_regexp uncached_terminal_string create_terminal_node create_non_terminal_node uncached_node terminal node put_in_sequence cached? cached cache pretty_print_cache}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#class_nameObject

:nodoc:



39
40
41
# File 'lib/textpeg2rubypeg.rb', line 39

def class_name
  @class_name
end

#rubyObject

:nodoc:



39
40
41
# File 'lib/textpeg2rubypeg.rb', line 39

def ruby
  @ruby
end

#tabsObject

:nodoc:



39
40
41
# File 'lib/textpeg2rubypeg.rb', line 39

def tabs
  @tabs
end

Class Method Details

.parse_file_to_loaded_class(filename) ⇒ Object



35
36
37
# File 'lib/textpeg2rubypeg.rb', line 35

def TextPeg2RubyPeg.parse_file_to_loaded_class(filename)
  parse_to_loaded_class IO.readlines(filename).join
end

.parse_to_loaded_class(text_peg) ⇒ Object



28
29
30
31
32
33
# File 'lib/textpeg2rubypeg.rb', line 28

def TextPeg2RubyPeg.parse_to_loaded_class(text_peg)
  builder = TextPeg2RubyPeg.new
  ruby =  TextPeg.parse(text_peg).visit(builder)
  Kernel.eval(ruby)
  Kernel.eval(builder.class_name)
end

.parse_to_ruby(text_peg) ⇒ Object



24
25
26
# File 'lib/textpeg2rubypeg.rb', line 24

def TextPeg2RubyPeg.parse_to_ruby(text_peg)
  TextPeg.parse(text_peg).visit(TextPeg2RubyPeg.new)
end

Instance Method Details

#alternatives(*elements) ⇒ Object

:nodoc:



138
139
140
# File 'lib/textpeg2rubypeg.rb', line 138

def alternatives(*elements) #:nodoc:
  elements.map { |e| e.visit(self) }.join(" || ")    
end

#any_characterObject

:nodoc:



158
159
160
# File 'lib/textpeg2rubypeg.rb', line 158

def any_character #:nodoc:
  "any_character"
end

#any_number_of(element) ⇒ Object

:nodoc:



130
131
132
# File 'lib/textpeg2rubypeg.rb', line 130

def any_number_of(element) #:nodoc:
  "any_number_of { #{element.visit(self)} }"
end

#bracketed_expression(expression) ⇒ Object

:nodoc:



142
143
144
# File 'lib/textpeg2rubypeg.rb', line 142

def bracketed_expression(expression) #:nodoc:
  "(#{expression.visit(self)})"
end

#close_classObject

:nodoc:



162
163
164
165
# File 'lib/textpeg2rubypeg.rb', line 162

def close_class #:nodoc:
  outdent
  line "end\n"
end

#define_class(name) ⇒ Object

:nodoc:



90
91
92
93
94
95
96
97
98
99
# File 'lib/textpeg2rubypeg.rb', line 90

def define_class(name) #:nodoc:
  self.class_name = name.to_class_name
  line "# encoding: utf-8"
  line "require 'rubypeg'"
  line ""
  line "class #{class_name} < RubyPeg"
  indent
  line
  @first_definition  = false
end

#define_root(name) ⇒ Object

:nodoc:



101
102
103
104
105
106
107
108
# File 'lib/textpeg2rubypeg.rb', line 101

def define_root(name) #:nodoc:
  line "def root"
  indent
  line name.to_method_name
  outdent
  line "end"
  line
end

#definition(identifier, expression) ⇒ Object

:nodoc:



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/textpeg2rubypeg.rb', line 57

def definition(identifier,expression) #:nodoc:
  non_clashing_name = identifier.visit(self)
  unless class_name
    define_class non_clashing_name
    define_root non_clashing_name
  end
  line "def #{non_clashing_name.to_method_name}"
  indent
  line expression.visit(self)
  outdent
  line "end"
  line
end

#followed_by(element) ⇒ Object

:nodoc:



114
115
116
# File 'lib/textpeg2rubypeg.rb', line 114

def followed_by(element) #:nodoc:
  "followed_by { #{element.visit(self)} }"
end

#identifier(name) ⇒ Object

:nodoc:



43
44
45
46
47
# File 'lib/textpeg2rubypeg.rb', line 43

def identifier(name) #:nodoc:
  return name.to_s unless RESERVED_WORDS.include?(name.to_s)
  $stderr.puts "Identifier #{name} clashes with a reserved word in the parser, replacing with _#{name}"
  "_#{name}"
end

#ignored(element) ⇒ Object

:nodoc:



118
119
120
# File 'lib/textpeg2rubypeg.rb', line 118

def ignored(element) #:nodoc:
  "ignore { #{element.visit(self)} }"
end

#indentObject

:nodoc:



171
172
173
# File 'lib/textpeg2rubypeg.rb', line 171

def indent #:nodoc:
  self.tabs = tabs + 1
end

#line(string = "") ⇒ Object

:nodoc:



167
168
169
# File 'lib/textpeg2rubypeg.rb', line 167

def line(string = "") #:nodoc:
  ruby << "#{"  "*tabs}#{string}"
end

#node(identifier, expression) ⇒ Object

:nodoc:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/textpeg2rubypeg.rb', line 71

def node(identifier,expression) #:nodoc:
  original_name = identifier.to_s
  non_clashing_name = identifier.visit(self)
  unless class_name
    define_class non_clashing_name
    define_root non_clashing_name
  end
  line "def #{non_clashing_name.to_method_name}"
  indent
  line "node :#{original_name.to_method_name} do"
  indent
  line expression.visit(self)
  outdent
  line "end"
  outdent
  line "end"
  line
end

#not_followed_by(element) ⇒ Object

:nodoc:



110
111
112
# File 'lib/textpeg2rubypeg.rb', line 110

def not_followed_by(element) #:nodoc:
  "not_followed_by { #{element.visit(self)} }"
end

#one_or_more(element) ⇒ Object

:nodoc:



126
127
128
# File 'lib/textpeg2rubypeg.rb', line 126

def one_or_more(element) #:nodoc:
  "one_or_more { #{element.visit(self)} }"
end

#optional(element) ⇒ Object

:nodoc:



122
123
124
# File 'lib/textpeg2rubypeg.rb', line 122

def optional(element) #:nodoc:
  "optional { #{element.visit(self)} }"
end

#outdentObject

:nodoc:



175
176
177
# File 'lib/textpeg2rubypeg.rb', line 175

def outdent #:nodoc:
  self.tabs = tabs - 1
end

#sequence(*elements) ⇒ Object

:nodoc:



134
135
136
# File 'lib/textpeg2rubypeg.rb', line 134

def sequence(*elements) #:nodoc:
  elements.map { |e| e.visit(self) }.join(" && ")
end

#terminal_character_range(regexp) ⇒ Object

:nodoc:



154
155
156
# File 'lib/textpeg2rubypeg.rb', line 154

def terminal_character_range(regexp) #:nodoc:
  "terminal(/#{regexp.visit(self)}/)"
end

#terminal_regexp(regexp) ⇒ Object

:nodoc:



150
151
152
# File 'lib/textpeg2rubypeg.rb', line 150

def terminal_regexp(regexp) #:nodoc:
  "terminal(/#{regexp.visit(self)}/)"
end

#terminal_string(string) ⇒ Object

:nodoc:



146
147
148
# File 'lib/textpeg2rubypeg.rb', line 146

def terminal_string(string) #:nodoc:
  %Q{terminal(#{string.visit(self).inspect})}
end

#text_peg(*definitions) ⇒ Object

:nodoc:



49
50
51
52
53
54
55
# File 'lib/textpeg2rubypeg.rb', line 49

def text_peg(*definitions) #:nodoc:
  self.ruby = []
  self.tabs = 0
  definitions.map { |d| d.visit(self) }
  close_class
  to_ruby
end

#to_rubyObject

:nodoc:



179
180
181
# File 'lib/textpeg2rubypeg.rb', line 179

def to_ruby #:nodoc:
  ruby.join("\n")
end