|
1 | 1 | module ParseUnparseWKTCRS |
2 | | - export GrammarSymbolKinds, ParserIdents |
| 2 | + export GrammarSymbolKinds, TokenUtil, ParserIdents |
3 | 3 | module GrammarSymbolKinds |
4 | 4 | export GrammarSymbolKind, grammar_symbol_error_kinds |
5 | 5 | using ParseUnparse.KindConstruction |
@@ -47,7 +47,7 @@ module ParseUnparseWKTCRS |
47 | 47 | ) |
48 | 48 | end |
49 | 49 | module TokenIterators |
50 | | - export TokenIterator |
| 50 | + export TokenIterator, encode_string, decode_string |
51 | 51 | using ParseUnparse.LexingUtil, ..GrammarSymbolKinds |
52 | 52 | struct TokenIterator{ListDelimiters, T} |
53 | 53 | character_iterator::T |
@@ -342,6 +342,99 @@ module ParseUnparseWKTCRS |
342 | 342 | end |
343 | 343 | end |
344 | 344 | end |
| 345 | + function encode_string_single_char(out::IO, decoded::AbstractChar) |
| 346 | + q = only(significant_characters.general.double_quote) |
| 347 | + print(out, decoded) |
| 348 | + if decoded == q |
| 349 | + print(out, decoded) |
| 350 | + end |
| 351 | + end |
| 352 | + function encode_string_no_quotes(out::IO, decoded) |
| 353 | + foreach(Base.Fix1(encode_string_single_char, out), decoded) |
| 354 | + end |
| 355 | + """ |
| 356 | + encode_string(out::IO, decoded)::Nothing |
| 357 | +
|
| 358 | + Encode the `decoded` iterator as a WKT-CRS string, outputting to `out`. |
| 359 | + """ |
| 360 | + function encode_string(out::IO, decoded) |
| 361 | + q = only(significant_characters.general.double_quote) |
| 362 | + print(out, q) |
| 363 | + encode_string_no_quotes(out, decoded) |
| 364 | + print(out, q) |
| 365 | + nothing |
| 366 | + end |
| 367 | + """ |
| 368 | + decode_string(out::IO, encoded)::Bool |
| 369 | +
|
| 370 | + Decode the `encoded` iterator, interpreted as a WKT-CRS string, outputting to `out`. |
| 371 | +
|
| 372 | + Return `true` if and only if no error was encountered. |
| 373 | + """ |
| 374 | + function decode_string(out::IO, encoded) |
| 375 | + lexer_state = let ols = lexer_state_simple_new(encoded) |
| 376 | + if ols === () |
| 377 | + return false |
| 378 | + end |
| 379 | + only(ols) |
| 380 | + end |
| 381 | + dquot = significant_characters.general.double_quote |
| 382 | + # This is a finite-state machine just like in `lex_string!`, but starting |
| 383 | + # with an extra state to skip initial white space and ending with an extra |
| 384 | + # state to check there's nothing except for white space at the end. |
| 385 | + while true |
| 386 | + oc = lexer_state_consume!(lexer_state) |
| 387 | + if isempty(oc) |
| 388 | + return false |
| 389 | + end |
| 390 | + c = only(oc) |
| 391 | + if c ∈ dquot |
| 392 | + break |
| 393 | + end |
| 394 | + if c ∉ significant_characters.general.whitespace |
| 395 | + return false |
| 396 | + end |
| 397 | + end |
| 398 | + # state 1 is merged into the above |
| 399 | + @label state_2 |
| 400 | + let oc = lexer_state_consume!(lexer_state) |
| 401 | + if isempty(oc) |
| 402 | + return false |
| 403 | + end |
| 404 | + c = only(oc) |
| 405 | + if character_does_not_need_escaping(c) |
| 406 | + print(out, c) |
| 407 | + @goto state_2 |
| 408 | + end |
| 409 | + end |
| 410 | + # state 3, accepting state |
| 411 | + let oc = lexer_state_peek!(lexer_state) |
| 412 | + if isempty(oc) |
| 413 | + return true |
| 414 | + end |
| 415 | + c = only(oc) |
| 416 | + if !character_does_not_need_escaping(c) |
| 417 | + print(out, c) |
| 418 | + lexer_state_consume!(lexer_state) |
| 419 | + @goto state_2 |
| 420 | + end |
| 421 | + end |
| 422 | + # trailing whitespace |
| 423 | + while true |
| 424 | + oc = lexer_state_consume!(lexer_state) |
| 425 | + if isempty(oc) |
| 426 | + break |
| 427 | + end |
| 428 | + if only(oc) ∉ significant_characters.general.whitespace |
| 429 | + return false |
| 430 | + end |
| 431 | + end |
| 432 | + true |
| 433 | + end |
| 434 | + end |
| 435 | + module TokenUtil |
| 436 | + export encode_string, decode_string |
| 437 | + using ..TokenIterators |
345 | 438 | end |
346 | 439 | module ParserIdents |
347 | 440 | export ParserIdent |
|
0 commit comments