| 1 |
# frozen-string-literal: true |
| 2 |
|
| 3 |
require 'strscan' |
| 4 |
|
| 5 |
module Tomlrb |
| 6 |
class Scanner |
| 7 |
COMMENT = |
| 8 |
/#[^\u0000-\u0008\u000A-\u001F\u007F]*/.freeze |
| 9 |
IDENTIFIER = |
| 10 |
/[A-Za-z0-9_-]+/.freeze |
| 11 |
SPACE = |
| 12 |
/[ \t]/.freeze |
| 13 |
NEWLINE = |
| 14 |
/(?:[ \t]*(?:\r?\n)[ \t]*)+/.freeze |
| 15 |
STRING_BASIC = |
| 16 |
/(")(?:\\?[^\u0000-\u0008\u000A-\u001F\u007F\\]|(?:\\[^\u0000-\u0008\u000A-\u001F\u007F]))*?\1/.freeze |
| 17 |
STRING_MULTI = |
| 18 |
/"{3}([^\u0000-\u0008\u000B\u000C\u000E-\u001F\u007F]*?(?<!\\)"{3,5})/m.freeze |
| 19 |
STRING_LITERAL = |
| 20 |
/(')(?:\\?[^\u0000-\u0008\u000A-\u001F\u007F])*?\1/.freeze |
| 21 |
STRING_LITERAL_MULTI = |
| 22 |
/'{3}([^\u0000-\u0008\u000B\u000C\u000E-\u001F\u007F]*?'{3,5})/m.freeze |
| 23 |
DATETIME = |
| 24 |
/(-?\d{4})-([01]\d)-([0-3]\d)(?:(?:t|\s)([0-2]\d):([0-5]\d):([0-6]\d(?:\.\d+)?))?(z|[-+][01]\d:\d{2})?/i.freeze |
| 25 |
LOCAL_TIME = |
| 26 |
/([0-2]\d):([0-5]\d):([0-6]\d(?:\.\d+)?)/.freeze |
| 27 |
FLOAT = |
| 28 |
/[+-]?(?:(?:\d|[1-9](?:_?\d)*)\.\d(?:_?\d)*|\d+(?=[eE]))(?:[eE][+-]?[0-9]+(_[0-9])*[0-9]*)?(?!\w)/.freeze |
| 29 |
FLOAT_KEYWORD = |
| 30 |
/[+-]?(?:inf|nan)\b/.freeze |
| 31 |
INTEGER = |
| 32 |
/[+-]?([1-9](_?\d)*|0)(?![A-Za-z0-9_-]+)/.freeze |
| 33 |
NON_DEC_INTEGER = |
| 34 |
/0(?:x[0-9A-Fa-f](_?[0-9A-Fa-f])*|o[0-7](_?[0-7])*|b[01](_?[01])*)/.freeze |
| 35 |
BOOLEAN = |
| 36 |
/true|false/.freeze |
| 37 |
SPACED_ARRAY_OF_TABLES_START = |
| 38 |
/^\[[ \t]+\[(#{IDENTIFIER}|#{STRING_BASIC}|#{STRING_LITERAL}|#{INTEGER}|#{NON_DEC_INTEGER}|#{FLOAT_KEYWORD}|#{BOOLEAN})\]\]$/.freeze |
| 39 |
SPACED_ARRAY_OF_TABLES_END = |
| 40 |
/^\[\[(#{IDENTIFIER}|#{STRING_BASIC}|#{STRING_LITERAL}|#{INTEGER}|#{NON_DEC_INTEGER}|#{FLOAT_KEYWORD}|#{BOOLEAN})\][ \t]+\]$/.freeze |
| 41 |
SPACED_ARRAY_OF_TABLES_BOTH = |
| 42 |
/^\[[ \t]+\[(#{IDENTIFIER}|#{STRING_BASIC}|#{STRING_LITERAL}|#{INTEGER}|#{NON_DEC_INTEGER}|#{FLOAT_KEYWORD}|#{BOOLEAN})\][ \t]+\]$/.freeze |
| 43 |
|
| 44 |
def initialize(io) |
| 45 |
@ss = StringScanner.new(io.read) |
| 46 |
@eos = false |
| 47 |
end |
| 48 |
|
| 49 |
def next_token |
| 50 |
case |
| 51 |
when @ss.scan(NEWLINE) then [:NEWLINE, nil] |
| 52 |
when @ss.scan(SPACED_ARRAY_OF_TABLES_START) then raise ParseError.new('Array of tables has spaces in starting brackets') |
| 53 |
when @ss.scan(SPACED_ARRAY_OF_TABLES_END) then raise ParseError.new('Array of tables has spaces in ending brackets') |
| 54 |
when @ss.scan(SPACED_ARRAY_OF_TABLES_BOTH) then raise ParseError.new('Array of tables has spaces in starting and ending brackets') |
| 55 |
when @ss.scan(SPACE) then next_token |
| 56 |
when @ss.scan(COMMENT) then next_token |
| 57 |
when @ss.scan(DATETIME) then process_datetime |
| 58 |
when @ss.scan(LOCAL_TIME) then process_local_time |
| 59 |
when text = @ss.scan(STRING_MULTI) then [:STRING_MULTI, text[3..-4]] |
| 60 |
when text = @ss.scan(STRING_BASIC) then [:STRING_BASIC, text[1..-2]] |
| 61 |
when text = @ss.scan(STRING_LITERAL_MULTI) then [:STRING_LITERAL_MULTI, text[3..-4]] |
| 62 |
when text = @ss.scan(STRING_LITERAL) then [:STRING_LITERAL, text[1..-2]] |
| 63 |
when text = @ss.scan(FLOAT) then [:FLOAT, text] |
| 64 |
when text = @ss.scan(FLOAT_KEYWORD) then [:FLOAT_KEYWORD, text] |
| 65 |
when text = @ss.scan(INTEGER) then [:INTEGER, text] |
| 66 |
when text = @ss.scan(NON_DEC_INTEGER) then [:NON_DEC_INTEGER, text] |
| 67 |
when text = @ss.scan(BOOLEAN) then [:BOOLEAN, text] |
| 68 |
when text = @ss.scan(IDENTIFIER) then [:IDENTIFIER, text] |
| 69 |
when @ss.eos? then process_eos |
| 70 |
else x = @ss.getch; [x, x] |
| 71 |
end |
| 72 |
end |
| 73 |
|
| 74 |
def process_datetime |
| 75 |
offset = @ss[7].gsub(/[zZ]/, '+00:00') if @ss[7] |
| 76 |
args = [@ss[0], @ss[1], @ss[2], @ss[3], @ss[4], @ss[5], @ss[6], offset] |
| 77 |
[:DATETIME, args] |
| 78 |
end |
| 79 |
|
| 80 |
def process_local_time |
| 81 |
args = [@ss[0], @ss[1], @ss[2], @ss[3].to_f] |
| 82 |
[:LOCAL_TIME, args] |
| 83 |
end |
| 84 |
|
| 85 |
def process_eos |
| 86 |
return if @eos |
| 87 |
|
| 88 |
@eos = true |
| 89 |
[:EOS, nil] |
| 90 |
end |
| 91 |
end |
| 92 |
end |