Class: Steamd::Parser

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

Overview

Parses steam language files. Give access to the parsed data via #classes, #imports and #enums methods.

Examples:

Parsing a Steam Language file

parser = Parser.new
parser.load! # loads the grammars, sets up the parser
parser.parse(File.open('./message.steamd'))

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

Instance Method Details

#classesArray<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

Examples:

Accessing the classes

parser.classes # => [{ name: 'test', variables: [], ...]

Returns:

  • (Array<Hash>)

    an array of hashes representing the class

Raises:

See Also:



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

#enumsArray<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

Examples:

Accessing the enums

parser.enums # => [{ name: 'test', variables: [], ...]

Returns:

  • (Array<Hash>)

    an array of hashes representing the class

Raises:

See Also:



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

#importsArray<Hash>

An array of hashes containing the imports found in the parsed Steam Language file.

The hash is the hash version of ImportStatement

Examples:

Accessing imports

parser.imports # => [{filename: 'test'}]

Returns:

  • (Array<Hash>)

    an array of hashes representing the imports found

Raises:

See Also:



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_errorObject Also known as: error

Returns a hash representing the reason the parser failed.

Examples:

parser.last_error # => { reason: "Missing ';'", column: 1, line: 12 }


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

Returns:

  • (Bool)

    returns true unless an exception is thrown



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.

Parameters:

  • io (#read, #rewind)

    An IO stream containing the Steam Language data.

Returns:

  • (Bool)

    returns true unless an error is thrown

Raises:



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 = strip_comments_and_obsolete_tags!(io)

  @tree = @parser.parse(data)
  raise_parser_error! if @tree.nil?

  true
end