Class: XMP2Assert::Parser
- Inherits:
-
Ripper
- Object
- Ripper
- XMP2Assert::Parser
- Defined in:
- lib/xmp2assert/parser.rb
Overview
This is a Ruby parser. Generates ASTs from the given program.
Instance Attribute Summary collapse
-
#sexp ⇒ Array
readonly
Constructed s-expression.
-
#tokens ⇒ Array<Token>
readonly
Program, split into tokens.
Instance Method Summary collapse
-
#initialize(obj, file = nil, line = nil) ⇒ Parser
constructor
A new instance of Parser.
-
#locations ⇒ String, Integer
The program's file name and line offset.
-
#same_line_as(tok) ⇒ Array
Find tokens that are in the same line as the argument.
Constructor Details
#initialize(obj, file = nil, line = nil) ⇒ Parser
Returns a new instance of Parser.
38 39 40 41 42 43 44 |
# File 'lib/xmp2assert/parser.rb', line 38 def initialize obj, file = nil, line = nil @qfile = XMP2Assert::Quasifile.new obj, file, line super @qfile.read, *locations @tokens = [] @sexp = parse @tokens.sort! end |
Instance Attribute Details
#sexp ⇒ Array (readonly)
Returns constructed s-expression.
34 35 36 |
# File 'lib/xmp2assert/parser.rb', line 34 def sexp @sexp end |
#tokens ⇒ Array<Token> (readonly)
Returns program, split into tokens.
33 34 35 |
# File 'lib/xmp2assert/parser.rb', line 33 def tokens @tokens end |
Instance Method Details
#locations ⇒ String, Integer
Returns the program's file name and line offset.
47 48 49 |
# File 'lib/xmp2assert/parser.rb', line 47 def locations return @qfile.__FILE__, @qfile.__LINE__ end |
#same_line_as(tok) ⇒ Array
Find tokens that are in the same line as the argument.
[ 1, # => 1
2, # => 2
] # => [1, 2]
It will return [(:sp ' ') (:int 2) (:'=>' "2")] for (:'=>' "2").
63 64 65 66 |
# File 'lib/xmp2assert/parser.rb', line 63 def same_line_as tok f, l = tok.__FILE__, tok.__LINE__ return @tokens.select {|i| i.__FILE__ == f }.select {|i| i.__LINE__ == l } end |