Class: Antelope::Ace::Compiler
- Inherits:
-
Object
- Object
- Antelope::Ace::Compiler
- Defined in:
- lib/antelope/ace/compiler.rb
Overview
Compiles a set of tokens generated by Scanner. These tokens may not nessicarily have been generated by Scanner, however the tokens must follow the same rules even still.
A list of all tokens that this compiler accepts:
:directive(2 arguments):copy(1 argument):second(no arguments):label(1 argument):part(1 argument):or(no arguments):prec(1 argument):block(1 argument):third(no arguments):body(1 argument)
The tokens are handled by methods that follow the rule
compile_<token name>.
Instance Attribute Summary collapse
-
#body ⇒ String
The body of the output compiler.
-
#options ⇒ Hash
Options defined by directives in the first part of the file.
-
#rules ⇒ Array<Hash>
A list of all the rules that are defined in the file.
Class Method Summary collapse
-
.compile(tokens) ⇒ Compiler
Creates a compiler, and then runs the compiler.
Instance Method Summary collapse
-
#compare_versions(required) ⇒ void
private
Compares the required version and the Antelope version.
-
#compile ⇒ self
Runs the compiler on the input tokens.
-
#compile_block(block) ⇒ void
Compiles a block.
-
#compile_copy(body) ⇒ void
Compiles a copy token.
-
#compile_directive(name, args) ⇒ void
Compiles a directive.
-
#compile_label(label) ⇒ void
Compiles a label.
-
#compile_or ⇒ void
Compiles an or.
-
#compile_part(text) ⇒ Object
Compiles a part.
-
#compile_prec(prec) ⇒ void
Compiles the precedence operator.
-
#compile_second ⇒ void
Sets the state to the second part.
-
#compile_third ⇒ void
Sets the state to the third part.
-
#initialize(tokens) ⇒ Compiler
constructor
Initialize the compiler.
-
#require_state!(*state) ⇒ void
private
Checks the current state against the given states.
Constructor Details
#initialize(tokens) ⇒ Compiler
Initialize the compiler. The compiler keeps track of a state;
this state is basically which part of the file we're in. The
state can be :first, :second, or :third; some tokens
may not exist in certain states.
86 87 88 89 90 91 92 93 94 |
# File 'lib/antelope/ace/compiler.rb', line 86 def initialize(tokens) @tokens = tokens @body = "" @state = :first @rules = [] @current = nil @current_label = nil = { :terminals => [], :prec => [], :extra => {} } end |
Instance Attribute Details
#body ⇒ String
The body of the output compiler. This should be formatted in the language that the parser is to be written in. Some output generators may have special syntax that allows the parser to be put in the body; see the output generators for more.
33 34 35 |
# File 'lib/antelope/ace/compiler.rb', line 33 def body @body end |
#options ⇒ Hash
Options defined by directives in the first part of the file.
:terminals(Array<Symbol, String?)>)— A list of all of the terminals in the language. If this is not properly defined, the grammar will throw an error saying that a symbol used in the grammar is not defined.:prec(Array<(Symbol, Array<Symbol>)>) — A list of the precedence rules of the grammar. The first element of each element is the type of precedence (and should be any of:left,:right, or:nonassoc), and the second element should be the symbols that are on that level.:type(String) — The type of generator to generate; this should be a language.:extra(Hash<Symbol, Array<Object>>) — Extra options that are not defined here.
68 69 70 |
# File 'lib/antelope/ace/compiler.rb', line 68 def end |
#rules ⇒ Array<Hash>
A list of all the rules that are defined in the file. The rules are defined as such:
label(Symbol) — The left-hand side of the rule; this is the nonterminal that the right side reduces to.set(Array<Symbol>) — The right-hand side of the rule. This is a combination of terminals and nonterminals.block(String) — The code to be run on a reduction. this should be formatted in the language that the output parser is written in. Optional; default value is"".prec(String) — The precedence level for the rule. This should be a nonterminal or terminal. Optional; default value is"".
50 51 52 |
# File 'lib/antelope/ace/compiler.rb', line 50 def rules @rules end |
Class Method Details
.compile(tokens) ⇒ Compiler
Creates a compiler, and then runs the compiler.
75 76 77 |
# File 'lib/antelope/ace/compiler.rb', line 75 def self.compile(tokens) new(tokens).compile end |
Instance Method Details
#compare_versions(required) ⇒ void (private)
This method returns an undefined value.
Compares the required version and the Antelope version.
276 277 278 279 280 281 282 283 284 285 |
# File 'lib/antelope/ace/compiler.rb', line 276 def compare_versions(required) antelope_version = Gem::Version.new(Antelope::VERSION) required_version = Gem::Requirement.new(required) unless required_version =~ antelope_version raise IncompatibleVersionError, "Grammar requires #{args[0]}, " \ "have #{Antelope::VERSION}" end end |
#compile ⇒ self
Runs the compiler on the input tokens. For each token,
it calls compile_<type> with <type> being the first
element of the token, with the remaining part of the array
passed as arguments.
102 103 104 105 106 107 108 |
# File 'lib/antelope/ace/compiler.rb', line 102 def compile @tokens.each do |token| send(:"compile_#{token[0]}", *token[1..-1]) end self end |
#compile_block(block) ⇒ void
This method returns an undefined value.
Compiles a block. This should only occur in a rule definition, and in the second part. It sets the block on the current rule.
239 240 241 242 |
# File 'lib/antelope/ace/compiler.rb', line 239 def compile_block(block) require_state! :second @current[:block] = block end |
#compile_copy(body) ⇒ void
This method returns an undefined value.
Compiles a copy token. A copy token basically copies its argument directly into the body. Used in both the first and third parts.
165 166 167 168 |
# File 'lib/antelope/ace/compiler.rb', line 165 def compile_copy(body) require_state! :first, :third @body << body end |
#compile_directive(name, args) ⇒ void
This method returns an undefined value.
Compiles a directive. This may only be triggered in the first section of the file. The directive accepts two arguments. The directive name can be any of the following:
:terminal— adds a terminal. Requires 1-2 arguments; the first argument is the terminal name, and the second argument is a string that can represent the terminal.:require— requires a certain version of Antelope. Requires 1 argument. If the first argument is a version greater than the current version of Antelope, it raises an error.:left— creates a new precedence level, with the argument values being the symbols. The precedence level is left associative.:right— creates a new precedence level, with the argument valeus being the symbols. The precedence level is right associative.:nonassoc— creates a nre precedence level, with the argument values being the symbols. The precedence level is nonassociative.:type— the type of parser to generate. This should correspond to the output language of the parser.
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/antelope/ace/compiler.rb', line 141 def compile_directive(name, args) require_state! :first name = name.intern case name when :terminal [:terminals] << [args[0].intern, args[1]] when :require compare_versions(args[0]) when :left, :right, :nonassoc [:prec] << [name, *args.map(&:intern)] when :type [:type] = args[0] else [:extra][name] = args $stderr.puts "Unknown Directive: #{name}" end end |
#compile_label(label) ⇒ void
This method returns an undefined value.
Compiles a label. This starts a rule definition. The token
should only exist in the second part. A rule definition
occurs by setting the @current_label to the first argument,
and @current to a blank rule save the label set. If a
rule definition was already in progress, it is completed.
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/antelope/ace/compiler.rb', line 186 def compile_label(label) require_state! :second if @current @rules << @current end @current_label = label.intern @current = { label: @current_label, set: [], block: "", prec: "" } end |
#compile_or ⇒ void
This method returns an undefined value.
Compiles an or. This should only occur in a rule definition, and in the second part. It starts a new rule definition by calling #compile_label with the current label.
218 219 220 |
# File 'lib/antelope/ace/compiler.rb', line 218 def compile_or compile_label(@current_label) end |
#compile_part(text) ⇒ Object
Compiles a part. This should only occur during a rule definition. The token should only exist in the second part. It adds the first argument to the set of the current rule.
207 208 209 210 |
# File 'lib/antelope/ace/compiler.rb', line 207 def compile_part(text) require_state! :second @current[:set] << text.intern end |
#compile_prec(prec) ⇒ void
This method returns an undefined value.
Compiles the precedence operator. This should only occur in a rule definition, and in the second part. It sets the precedence definition on the current rule.
228 229 230 231 |
# File 'lib/antelope/ace/compiler.rb', line 228 def compile_prec(prec) require_state! :second @current[:prec] = prec end |
#compile_second ⇒ void
This method returns an undefined value.
Sets the state to the second part.
173 174 175 |
# File 'lib/antelope/ace/compiler.rb', line 173 def compile_second @state = :second end |
#compile_third ⇒ void
This method returns an undefined value.
Sets the state to the third part. If a rule definition was in progress, it finishes the rule.
248 249 250 251 252 253 254 255 |
# File 'lib/antelope/ace/compiler.rb', line 248 def compile_third if @current @rules << @current @current_label = @current = nil end @state = :third end |
#require_state!(*state) ⇒ void (private)
This method returns an undefined value.
Checks the current state against the given states.
264 265 266 267 268 269 |
# File 'lib/antelope/ace/compiler.rb', line 264 def require_state!(*state) raise InvalidStateError, "In state #{@state}, " \ "required state #{state.join(", ")}" \ unless state.include?(@state) end |