Class: SportDb::Lexer::Token
- Inherits:
-
Object
- Object
- SportDb::Lexer::Token
- Defined in:
- lib/sportdb/parser/lexer_token.rb
Instance Attribute Summary collapse
-
#lineno ⇒ Object
readonly
Returns the value of attribute lineno.
-
#offset ⇒ Object
readonly
Returns the value of attribute offset.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
-
.literal(literal, lineno:, offset: []) ⇒ Object
Token.literal( “,”, lineno: 4, offset: [5,6]) # maps to => Token.new( “,”, “,”, lineno: 4, offset: [5,6]).
-
.newline(lineno:, offset: []) ⇒ Object
Token.newline( lineno: 1, offset: [1,2] ) maps to => Token.new( :NEWLINE, “n”, lineno: 1, offset: [1,2]).
-
.virtual(type, lineno:, offset: []) ⇒ Object
or use virt or pseudo - why? why not?.
Instance Method Summary collapse
- #as_ary ⇒ Object
- #as_hash ⇒ Object
- #as_int ⇒ Object
-
#as_str ⇒ Object
note: do NOT use as_text/text to avoid confusion with (raw) text (lexeme).
-
#initialize(type, text = '', lineno:, offset: [], value: nil) ⇒ Token
constructor
A new instance of Token.
-
#pretty_print(printer) ⇒ Object
pretty print.
- #to_legacy ⇒ Object
- #value ⇒ Object
Constructor Details
#initialize(type, text = '', lineno:, offset: [], value: nil) ⇒ Token
Returns a new instance of Token.
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/sportdb/parser/lexer_token.rb', line 37 def initialize( type, text='', lineno:, offset: [], value: nil ) @type = type @text = text # note - lexeme (string from source) @lineno = lineno # note - lineno (integer number - not line as string) !!! raise TypeError, "type Array required for offset; got #{offset.inspect}" unless offset.is_a?( Array ) @offset = offset # note - for now char offset [start,end] in line (NOT absolute!!) # maybe latter add MatchData#byteoffset instead - why? why not? @value = value # might be (union of) string/array/hash end |
Instance Attribute Details
#lineno ⇒ Object (readonly)
Returns the value of attribute lineno.
34 35 36 |
# File 'lib/sportdb/parser/lexer_token.rb', line 34 def lineno @lineno end |
#offset ⇒ Object (readonly)
Returns the value of attribute offset.
34 35 36 |
# File 'lib/sportdb/parser/lexer_token.rb', line 34 def offset @offset end |
#text ⇒ Object (readonly)
Returns the value of attribute text.
34 35 36 |
# File 'lib/sportdb/parser/lexer_token.rb', line 34 def text @text end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
34 35 36 |
# File 'lib/sportdb/parser/lexer_token.rb', line 34 def type @type end |
Class Method Details
.literal(literal, lineno:, offset: []) ⇒ Object
22 23 24 |
# File 'lib/sportdb/parser/lexer_token.rb', line 22 def self.literal( literal, lineno:, offset: []) new( literal, literal, lineno: lineno, offset: offset ) end |
.newline(lineno:, offset: []) ⇒ Object
13 14 15 |
# File 'lib/sportdb/parser/lexer_token.rb', line 13 def self.newline( lineno:, offset: []) new( :NEWLINE, "\n", lineno: lineno, offset: offset ) end |
.virtual(type, lineno:, offset: []) ⇒ Object
or use virt or pseudo - why? why not?
27 28 29 30 31 |
# File 'lib/sportdb/parser/lexer_token.rb', line 27 def self.virtual( type, lineno:, offset: []) ## note - offset (start/end) should be same number (zero-width assertions!!) ## e.g. :GOALS_COMPAT, "<|GOALS_COMPAT|>" new( type, '', lineno: lineno, offset: offset ) end |
Instance Method Details
#as_ary ⇒ Object
82 83 84 85 |
# File 'lib/sportdb/parser/lexer_token.rb', line 82 def as_ary raise TypeError, "token value #{@value.inspect} is #{@value.class.name} NOT array; sorry" if !@value.is_a?(Array) @value end |
#as_hash ⇒ Object
77 78 79 80 |
# File 'lib/sportdb/parser/lexer_token.rb', line 77 def as_hash raise TypeError, "token value #{@value.inspect} is #{@value.class.name} NOT hash; sorry" if !@value.is_a?(Hash) @value end |
#as_int ⇒ Object
72 73 74 75 |
# File 'lib/sportdb/parser/lexer_token.rb', line 72 def as_int raise TypeError, "token value #{@value.inspect} is #{@value.class.name} NOT integer; sorry" if !@value.is_a?(Integer) @value end |
#as_str ⇒ Object
note: do NOT use as_text/text to avoid confusion with (raw) text (lexeme)
use
as_str -- value (as String)
as_int -- value (as Integer)
as_hash -- value (as Hash)
as_ary -- value (as Array)
65 66 67 68 69 70 |
# File 'lib/sportdb/parser/lexer_token.rb', line 65 def as_str raise TypeError, "token value #{@value.inspect} is #{@value.class.name} NOT string; sorry" if @value && !@value.is_a?(String) ## note - if value is not set (nil) return text (lexeme) ## no need to duplicate text as value @value.nil? ? @text : @value end |
#pretty_print(printer) ⇒ Object
pretty print
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/sportdb/parser/lexer_token.rb', line 99 def pretty_print( printer ) ## check for literal e.g. "," etc. if @type.is_a?( String ) && @type == @text && @value.nil? printer.text( "[#{@type.inspect}" ) elsif @type.is_a?( Symbol ) && @text == '' && @value.nil? ## assume virtual token (zero-width) ## use <!...!> style printer.text( "[<|#{@type}|>" ) else printer.text( "[#{@type.inspect} #{@text.inspect}" ) printer.text( ", #{value.inspect}") if @value end printer.text( " @#{@lineno}" ) ## note - for now print only start_offset (offset[0]) ## to keep dump/output shorter ## note - start counting columns at one (NOT zero), thus, add +1 !! printer.text( ":#{@offset[0]+1}" ) if @offset.is_a?(Array) && @offset.size == 2 printer.text( "]" ) end |
#to_legacy ⇒ Object
88 89 90 91 92 93 94 95 |
# File 'lib/sportdb/parser/lexer_token.rb', line 88 def to_legacy ## return old "legacy" array format if @value.nil? [@type, @text] else [@type, [@text, @value]] end end |
#value ⇒ Object
50 51 52 53 54 |
# File 'lib/sportdb/parser/lexer_token.rb', line 50 def value ## note - if value is not set (nil) return text (lexeme) ## no need to duplicate text as value @value.nil? ? @text : @value end |