Class: Steamd::Parser
- Inherits:
-
Object
- Object
- Steamd::Parser
- Defined in:
- lib/steamd/parser.rb
Overview
Parses steam language files. Give access to the parsed data via #classes, #imports and #enums methods.
Constant Summary collapse
- Error =
The base Parser error
Class.new(StandardError)
- ParseError =
Thrown if there was any non specific Parse error.
Class.new(Error)
- NotLoadedError =
Thrown if the the parse method is called before the parser is loaded
Class.new(Error)
- StreamNotParsed =
Thrown if the callee tries to access the parsed data before parse is called
Class.new(Error)
Instance Method Summary collapse
-
#classes ⇒ Array<Hash>
Returns an array of classes parsed from the IO stream.
-
#enums ⇒ Array<Hash>
Returns an array of eneums parsed from the IO stream.
-
#imports ⇒ Array<Hash>
An array of hashes containing the imports found in the parsed Steam Language file.
-
#last_error ⇒ Object
(also: #error)
Returns a hash representing the reason the parser failed.
-
#load! ⇒ Bool
Load the grammars.
-
#parse(io) ⇒ Bool
Parses an IO stream.
Instance Method Details
#classes ⇒ Array<Hash>
Returns an array of classes parsed from the IO stream. The array contains hashes with class information and variables.
The hash is the hash form of ClassStatement
90 91 92 93 94 95 96 97 98 |
# File 'lib/steamd/parser.rb', line 90 def classes raise StreamNotParsed, 'you must parse first' if @tree.nil? classes = statements.select do |node| node.is_a?(ClassStatement) end classes.map(&:to_hash) end |
#enums ⇒ Array<Hash>
Returns an array of eneums parsed from the IO stream. The array contains hashes with enum information and variables.
The hash is the hash form of EnumStatement
110 111 112 113 114 115 116 117 118 |
# File 'lib/steamd/parser.rb', line 110 def enums raise StreamNotParsed, 'you must parse first' if @tree.nil? enums = statements.select do |node| node.is_a?(EnumStatement) end enums.map(&:to_hash) end |
#imports ⇒ Array<Hash>
An array of hashes containing the imports found in the parsed Steam Language file.
The hash is the hash version of ImportStatement
70 71 72 73 74 75 76 77 78 |
# File 'lib/steamd/parser.rb', line 70 def imports raise StreamNotParsed, 'you must parse first' if @tree.nil? imports = statements.select do |node| node.is_a?(ImportStatement) end imports.map(&:to_hash) end |
#last_error ⇒ Object Also known as: error
Returns a hash representing the reason the parser failed.
124 125 126 127 128 129 130 |
# File 'lib/steamd/parser.rb', line 124 def last_error { reason: @parser.failure_reason, column: @parser.failure_column, line: @parser.failure_line } end |
#load! ⇒ Bool
Load the grammars
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/steamd/parser.rb', line 30 def load! return if loaded? Treetop.load("#{Steamd.grammar_dir}/shared.treetop") Dir.glob("#{Steamd.grammar_dir}/*.treetop") do |file| Treetop.load(file) end @parser = SteamdParser.new true end |
#parse(io) ⇒ Bool
Parses an IO stream.
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/steamd/parser.rb', line 48 def parse(io) io.rewind raise NotLoadedError, 'load before parsing (#load!)' if @parser.nil? data = (io) @tree = @parser.parse(data) raise_parser_error! if @tree.nil? true end |