| |
|
|
| include "../../Wrappers.dfy" |
| include "../../BoundedInts.dfy" |
|
|
| module {:options "-functionSyntax:4"} JSON.Utils.Lexers { |
| module Core { |
| import opened Wrappers |
| import opened BoundedInts |
|
|
| datatype LexerResult<+T, +R> = |
| |
| | Accept |
| | Reject(err: R) |
| | Partial(st: T) |
|
|
| type Lexer<!T, +R> = (T, opt_byte) -> LexerResult<T, R> |
| } |
|
|
| module Strings { |
| import opened Core |
| import opened BoundedInts |
|
|
| type StringBodyLexerState = bool |
| const StringBodyLexerStart: StringBodyLexerState := false |
|
|
| function StringBody<R>(escaped: StringBodyLexerState, byte: opt_byte) |
| : LexerResult<StringBodyLexerState, R> |
| { |
| if byte == '\\' as opt_byte then Partial(!escaped) |
| else if byte == '\"' as opt_byte && !escaped then Accept |
| else Partial(false) |
| } |
|
|
| datatype StringLexerState = Start | Body(escaped: bool) | End |
| const StringLexerStart: StringLexerState := Start |
|
|
| function String(st: StringLexerState, byte: opt_byte) |
| : LexerResult<StringLexerState, string> |
| { |
| match st |
| case Start() => |
| if byte == '\"' as opt_byte then Partial(Body(false)) |
| else Reject("String must start with double quote") |
| case End() => |
| Accept |
| case Body(escaped) => |
| if byte == '\\' as opt_byte then Partial(Body(!escaped)) |
| else if byte == '\"' as opt_byte && !escaped then Partial(End) |
| else Partial(Body(false)) |
| } |
| } |
| } |
| |