Class: Jsonerino::Lexer
- Inherits:
-
Object
- Object
- Jsonerino::Lexer
- Defined in:
- lib/jsonerino/lexer.rb
Constant Summary collapse
- SIMPLE_TOKENS_MATCHER =
[ ['{', Token::TOKEN_LCURLY], ['}', Token::TOKEN_RCURLY], ['[', Token::TOKEN_LBRACKET], [']', Token::TOKEN_RBRACKET], [':', Token::TOKEN_COLON], [',', Token::TOKEN_COMMA] ].freeze
- SIMPLE_ESCAPE_SEQUENCES_MATCHER =
['"', '"'], ['b', "\b"], ['f', "\f"], ['r', "\r"], %W[n \n], %W[t \t] ].freeze
Instance Attribute Summary collapse
-
#i ⇒ Object
readonly
Returns the value of attribute i.
Instance Method Summary collapse
-
#initialize(contents) ⇒ Lexer
constructor
A new instance of Lexer.
- #next_token ⇒ Object
Constructor Details
#initialize(contents) ⇒ Lexer
Returns a new instance of Lexer.
26 27 28 29 30 31 32 |
# File 'lib/jsonerino/lexer.rb', line 26 def initialize(contents) @contents = contents @i = 0 @l = 1 # Line number (row) @li = 1 # Character in current line (column) @c = contents[@i] end |
Instance Attribute Details
#i ⇒ Object (readonly)
Returns the value of attribute i.
24 25 26 |
# File 'lib/jsonerino/lexer.rb', line 24 def i @i end |
Instance Method Details
#next_token ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/jsonerino/lexer.rb', line 34 def next_token skip_whitespace while more_content? v = collect_simple_token return v if v # Numbers in JSON are only allowed to start with a minus sign return collect_number if number? return collect_string if @c == '"' return collect_id if Helpers.alphanumeric?(@c) raise "Unexpected character '#{@c}'" end nil end |