Class: Sass::Script::Lexer
- Inherits:
-
Object
- Object
- Sass::Script::Lexer
- Includes:
- Sass::SCSS::RX
- Defined in:
- /var/www/sass-pages/.sass/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.
Direct Known Subclasses
Constant Summary
Instance Attribute Summary
- - (Fixnum) line readonly The line number of the lexer’s current position.
- - (Fixnum) offset readonly The number of bytes into the current line of the lexer’s current position.
Instance Method Summary
-
- (Boolean) after_interpolation?
Whether or not the last token lexed was
:end_interpolation. - - (Boolean) done? Whether or not there’s more source text to lex.
-
- expected!(name)
Raise an error to the effect that
namewas expected in the input stream and wasn’t found. - - (Lexer) initialize(str, line, offset, options) constructor A new instance of Lexer.
- - (Token) next Moves the lexer forward one token.
- - (Token) peek Returns the next token without moving the lexer forward.
- - (String) str { ... } Records all non-comment text the lexer consumes within the block and returns it as a string.
- - unpeek! Rewinds the underlying StringScanner to before the token returned by #peek.
- - (Boolean) whitespace?(tok = @tok) Returns whether or not there’s whitespace before the next token.
Methods included from Sass::SCSS::RX
Constructor Details
- (Lexer) initialize(str, line, offset, options)
A new instance of Lexer
126 127 128 129 130 131 132 133 |
# File '/var/www/sass-pages/.sass/lib/sass/script/lexer.rb', line 126
def initialize(str, line, offset, options)
@scanner = str.is_a?(StringScanner) ? str : Sass::Util::MultibyteStringScanner.new(str)
@line = line
@offset = offset
@options = options
@interpolation_stack = []
@prev = nil
end
|
Instance Attribute Details
- (Fixnum) line (readonly)
The line number of the lexer’s current position.
32 33 34 |
# File '/var/www/sass-pages/.sass/lib/sass/script/lexer.rb', line 32
def line
@line
end
|
- (Fixnum) offset (readonly)
The number of bytes into the current line of the lexer’s current position.
38 39 40 |
# File '/var/www/sass-pages/.sass/lib/sass/script/lexer.rb', line 38
def offset
@offset
end
|
Instance Method Details
- (Boolean) after_interpolation?
Whether or not the last token lexed was :end_interpolation.
177 178 179 |
# File '/var/www/sass-pages/.sass/lib/sass/script/lexer.rb', line 177
def after_interpolation?
@prev && @prev.type == :end_interpolation
end
|
- (Boolean) done?
Whether or not there’s more source text to lex.
171 172 173 174 |
# File '/var/www/sass-pages/.sass/lib/sass/script/lexer.rb', line 171
def done?
whitespace unless after_interpolation? && @interpolation_stack.last
@scanner.eos? && @tok.nil?
end
|
- expected!(name)
Raise an error to the effect that name was expected in the input stream and wasn’t found.
This calls #unpeek! to rewind the scanner to immediately after the last returned token.
189 190 191 192 |
# File '/var/www/sass-pages/.sass/lib/sass/script/lexer.rb', line 189
def expected!(name)
unpeek!
Sass::SCSS::Parser.expected(@scanner, name, @line)
end
|
- (Token) next
Moves the lexer forward one token.
138 139 140 141 142 143 |
# File '/var/www/sass-pages/.sass/lib/sass/script/lexer.rb', line 138
def next
@tok ||= read_token
@tok, tok = nil, @tok
@prev = tok
return tok
end
|
- (Token) peek
Returns the next token without moving the lexer forward.
160 161 162 |
# File '/var/www/sass-pages/.sass/lib/sass/script/lexer.rb', line 160
def peek
@tok ||= read_token
end
|
- (String) str { ... }
Records all non-comment text the lexer consumes within the block and returns it as a string.
199 200 201 202 203 204 |
# File '/var/www/sass-pages/.sass/lib/sass/script/lexer.rb', line 199
def str
old_pos = @tok ? @tok.pos : @scanner.pos
yield
new_pos = @tok ? @tok.pos : @scanner.pos
@scanner.string[old_pos...new_pos]
end
|
- unpeek!
Rewinds the underlying StringScanner to before the token returned by #peek.
166 167 168 |
# File '/var/www/sass-pages/.sass/lib/sass/script/lexer.rb', line 166
def unpeek!
@scanner.pos = @tok.pos if @tok
end
|
- (Boolean) whitespace?(tok = @tok)
Returns whether or not there’s whitespace before the next token.
148 149 150 151 152 153 154 155 |
# File '/var/www/sass-pages/.sass/lib/sass/script/lexer.rb', line 148
def whitespace?(tok = @tok)
if tok
@scanner.string[0...tok.pos] =~ /\s\Z/
else
@scanner.string[@scanner.pos, 1] =~ /^\s/ ||
@scanner.string[@scanner.pos - 1, 1] =~ /\s\Z/
end
end
|