Class: XMP2Assert::Token

Inherits:
Struct
  • Object
show all
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

Token locations collapse

Instance Method Summary collapse

Methods included from PrettierInspect

#inspect

Instance Attribute Details

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

#yyllocArray



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

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