Class: XMP2Assert::Token
- Inherits:
-
Struct
- Object
- Struct
- XMP2Assert::Token
- Includes:
- Comparable, PrettierInspect
- Defined in:
- lib/xmp2assert/token.rb
Overview
Token is a tiny class that represents a token of a Ruby program.
Instance Attribute Summary collapse
-
#yylex ⇒ Symbol
(also: #to_sym)
Terminal symbol.
-
#yylloc ⇒ Array
Terminal location.
-
#yylval ⇒ String
(also: #to_s)
Terminal value.
Token locations collapse
-
#__COLUMN__ ⇒ String
Column in a line.
-
#__FILE__ ⇒ String
File name.
-
#__LINE__ ⇒ String
Line number (1 origin).
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Comparison of location in a file, to be used with sort.
- #pretty_print(pp) ⇒ Object
-
#raise(klass = SyntaxError, msg = "") ⇒ Object
Considet this token being an error.
Methods included from PrettierInspect
Instance Attribute Details
#yylex ⇒ Symbol Also known as: to_sym
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/xmp2assert/token.rb', line 37 XMP2Assert::Token = Struct.new :yylex, :yylval, :yylloc do include Comparable # Comparison of location in a file, to be used with sort. # @param other [Token] token to compare def <=> other yylloc <=> other.yylloc end alias to_sym yylex alias to_s yylval # @!group Token locations # @return [String] file name def __FILE__ return yylloc[0] end # @return [String] line number (1 origin) def __LINE__ return yylloc[1] end # @return [String] column in a line def __COLUMN__ return yylloc[2] end # @!endgroup # Considet this token being an error. # @param klass [Exception] exception to raise # @param msg [String] diagnostic message def raise klass = SyntaxError, msg = "" l = sprintf "%s:%s", self.__FILE__, self.__LINE__ m = sprintf 'syntax error near "%s" at line %d:%d %s', to_s, self.__LINE__, self.__COLUMN__, msg super klass, m, [l, *caller] end unless $DEBUG include ::XMP2Assert::PrettierInspect def pretty_print pp pp.text "(" yylex.pretty_print pp pp.breakable " " yylval.pretty_print pp pp.text ")" end end end |
#yylloc ⇒ Array
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/xmp2assert/token.rb', line 37 XMP2Assert::Token = Struct.new :yylex, :yylval, :yylloc do include Comparable # Comparison of location in a file, to be used with sort. # @param other [Token] token to compare def <=> other yylloc <=> other.yylloc end alias to_sym yylex alias to_s yylval # @!group Token locations # @return [String] file name def __FILE__ return yylloc[0] end # @return [String] line number (1 origin) def __LINE__ return yylloc[1] end # @return [String] column in a line def __COLUMN__ return yylloc[2] end # @!endgroup # Considet this token being an error. # @param klass [Exception] exception to raise # @param msg [String] diagnostic message def raise klass = SyntaxError, msg = "" l = sprintf "%s:%s", self.__FILE__, self.__LINE__ m = sprintf 'syntax error near "%s" at line %d:%d %s', to_s, self.__LINE__, self.__COLUMN__, msg super klass, m, [l, *caller] end unless $DEBUG include ::XMP2Assert::PrettierInspect def pretty_print pp pp.text "(" yylex.pretty_print pp pp.breakable " " yylval.pretty_print pp pp.text ")" end end end |
#yylval ⇒ String Also known as: to_s
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/xmp2assert/token.rb', line 37 XMP2Assert::Token = Struct.new :yylex, :yylval, :yylloc do include Comparable # Comparison of location in a file, to be used with sort. # @param other [Token] token to compare def <=> other yylloc <=> other.yylloc end alias to_sym yylex alias to_s yylval # @!group Token locations # @return [String] file name def __FILE__ return yylloc[0] end # @return [String] line number (1 origin) def __LINE__ return yylloc[1] end # @return [String] column in a line def __COLUMN__ return yylloc[2] end # @!endgroup # Considet this token being an error. # @param klass [Exception] exception to raise # @param msg [String] diagnostic message def raise klass = SyntaxError, msg = "" l = sprintf "%s:%s", self.__FILE__, self.__LINE__ m = sprintf 'syntax error near "%s" at line %d:%d %s', to_s, self.__LINE__, self.__COLUMN__, msg super klass, m, [l, *caller] end unless $DEBUG include ::XMP2Assert::PrettierInspect def pretty_print pp pp.text "(" yylex.pretty_print pp pp.breakable " " yylval.pretty_print pp pp.text ")" end end end |
Instance Method Details
#<=>(other) ⇒ Object
Comparison of location in a file, to be used with sort.
42 43 44 |
# File 'lib/xmp2assert/token.rb', line 42 def <=> other yylloc <=> other.yylloc end |
#__COLUMN__ ⇒ String
62 63 64 |
# File 'lib/xmp2assert/token.rb', line 62 def __COLUMN__ return yylloc[2] end |
#__FILE__ ⇒ String
52 53 54 |
# File 'lib/xmp2assert/token.rb', line 52 def __FILE__ return yylloc[0] end |
#__LINE__ ⇒ String
57 58 59 |
# File 'lib/xmp2assert/token.rb', line 57 def __LINE__ return yylloc[1] end |
#pretty_print(pp) ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/xmp2assert/token.rb', line 80 def pretty_print pp pp.text "(" yylex.pretty_print pp pp.breakable " " yylval.pretty_print pp pp.text ")" end |
#raise(klass = SyntaxError, msg = "") ⇒ Object
Considet this token being an error.
70 71 72 73 74 75 |
# File 'lib/xmp2assert/token.rb', line 70 def raise klass = SyntaxError, msg = "" l = sprintf "%s:%s", self.__FILE__, self.__LINE__ m = sprintf 'syntax error near "%s" at line %d:%d %s', to_s, self.__LINE__, self.__COLUMN__, msg super klass, m, [l, *caller] end |