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:



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

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

#any_characterObject

:nodoc:



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

def any_character #:nodoc:
  "any_character"
end

#any_number_of(element) ⇒ Object

:nodoc:



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

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

#bracketed_expression(expression) ⇒ Object

:nodoc:



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

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

#close_classObject

:nodoc:



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

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

#define_class(name) ⇒ Object

:nodoc:



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

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

#define_root(name) ⇒ Object

:nodoc:



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

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:



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

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:



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

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

#indentObject

:nodoc:



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

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

#line(string = "") ⇒ Object

:nodoc:



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

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:



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

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

#one_or_more(element) ⇒ Object

:nodoc:



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

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

#optional(element) ⇒ Object

:nodoc:



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

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

#outdentObject

:nodoc:



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

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

#sequence(*elements) ⇒ Object

:nodoc:



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

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

#terminal_character_range(regexp) ⇒ Object

:nodoc:



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

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

#terminal_regexp(regexp) ⇒ Object

:nodoc:



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

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

#terminal_string(string) ⇒ Object

:nodoc:



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

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:



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

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