Class: Sass::Script::Lexer

Inherits:
Object show all
Defined in:
lib/sass/script/lexer.rb

Overview

The lexical analyzer for SassScript. It takes a raw string and converts it to individual tokens that are easier to parse.

Defined Under Namespace

Classes: Token

Constant Summary collapse

STRING_REGULAR_EXPRESSIONS =

A hash of regular expressions that are used for tokenizing strings.

The key is a [Symbol, Boolean] pair. The symbol represents which style of quotation to use, while the boolean represents whether or not the string is following an interpolated segment.

{
  [:double, false] => string_re('"', '"'),
  [:single, false] => string_re("'", "'"),
  [:double, true] => string_re('', '"'),
  [:single, true] => string_re('', "'"),
}

Instance Method Summary collapse

Constructor Details

#initialize(str, line, offset, options) ⇒ Lexer

Returns a new instance of Lexer.

Parameters:

  • str (String, StringScanner)

    The source text to lex

  • line (Fixnum)

    The line on which the SassScript appears. Used for error reporting

  • offset (Fixnum)

    The number of characters in on which the SassScript appears. Used for error reporting

  • options ({Symbol => Object})

    An options hash; see the Sass options documentation



93
94
95
96
97
98
99
100
# File 'lib/sass/script/lexer.rb', line 93

def initialize(str, line, offset, options)
  @scanner = str.is_a?(StringScanner) ? str : StringScanner.new(str)
  @line = line
  @offset = offset
  @options = options
  @interpolation_stack = []
  @prev = nil
end

Instance Method Details

#done?Boolean

Returns Whether or not there's more source text to lex.

Returns:

  • (Boolean)

    Whether or not there's more source text to lex.



120
121
122
123
# File 'lib/sass/script/lexer.rb', line 120

def done?
  whitespace unless after_interpolation?
  @scanner.eos? && @tok.nil?
end

#nextToken

Moves the lexer forward one token.

Returns:

  • (Token)

    The token that was moved past



105
106
107
108
109
110
# File 'lib/sass/script/lexer.rb', line 105

def next
  @tok ||= read_token
  @tok, tok = nil, @tok
  @prev = tok
  return tok
end

#peekToken

Returns the next token without moving the lexer forward.

Returns:

  • (Token)

    The next token



115
116
117
# File 'lib/sass/script/lexer.rb', line 115

def peek
  @tok ||= read_token
end