17-source-analysis/01-lossless-lexer.protos

Repository
guillermomolina/protos
Commit
2723a55555e7beecee0816bf99d16e3c8c1ae635
Tree
c36054e4e5abd7fc29a039b4e096cac2e8706492
Blob
769ca26ea57e5f7bfec8b9ec44f0c70c1eb9a9ba
Source
View exact source on GitHub
// THE LICENSED WORK IS PROVIDED UNDER THE TERMS OF THE ADAPTIVE PUBLIC LICENSE
// ("LICENSE") AS FIRST COMPLETED BY: Guillermo Adrián Molina. ANY USE, PUBLIC
// DISPLAY, PUBLIC PERFORMANCE, REPRODUCTION OR DISTRIBUTION OF, OR PREPARATION OF
// DERIVATIVE WORKS BASED ON, THE LICENSED WORK CONSTITUTES RECIPIENT'S ACCEPTANCE
// OF THIS LICENSE AND ITS TERMS, WHETHER OR NOT SUCH RECIPIENT READS THE TERMS OF
// THE LICENSE. "LICENSED WORK" AND "RECIPIENT" ARE DEFINED IN THE LICENSE. A COPY
// OF THE LICENSE IS LOCATED IN THE TEXT FILE ENTITLED "LICENSE.TXT" ACCOMPANYING
// THE CONTENTS OF THIS FILE. IF A COPY OF THE LICENSE DOES NOT ACCOMPANY THIS
// FILE, A COPY OF THE LICENSE MAY ALSO BE OBTAINED AT THE FOLLOWING WEB SITE:
// https://github.com/guillermomolina/protos
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
// the specific language governing rights and limitations under the License.

// Lossless lexing with std:source/lexer.
//
// Lexer.lex never executes source. It partitions the text into pieces: grammar
// tokens plus trivia (whitespace and comments). Concatenating every piece's
// rawText reproduces the source exactly. Positions are zero-based, half-open
// Unicode scalar offsets.

Lexer: import("std:source/lexer")

source: "total: 3 + 4 // sum\n"
result: Lexer.lex(source)

for(piece in result.pieces) {
    print(piece.kind + " [" + piece.rawText + "]")
}
// IDENTIFIER [total]
// COLON [:]
// HORIZONTAL_WHITESPACE [ ]
// NUMBER [3]
// HORIZONTAL_WHITESPACE [ ]
// PLUS [+]
// HORIZONTAL_WHITESPACE [ ]
// NUMBER [4]
// HORIZONTAL_WHITESPACE [ ]
// LINE_COMMENT [// sum]
// NEWLINE [
// ]
// EOF []

// The partition is exact: rebuild the source from rawText.
rebuilt: ""
for(piece in result.pieces) {
    rebuilt = rebuilt + piece.rawText
}
print(rebuilt == source)                                // true

// Exactly one EOF piece ends the Array, at [size, size).
last: result.pieces[result.pieces.size() - 1]
print(last.kind)                                        // EOF
print(last.start == source.size() && last.end == source.size())  // true

// A String piece keeps its written spelling in rawText and its decoded value
// in lexeme.
quoted: Lexer.lex("\"a\\tb\"").pieces[0]
print(quoted.rawText)                                   // "a\tb"
print(quoted.lexeme == "a\tb")                          // true

// Strict mode (the default) signals the first malformed region.
print(Lexer.LexicalError.handle(
    () => { Lexer.lex("x: \"open"); "accepted" },
    (error) => { error.code }
))                                                      // UNTERMINATED_STRING

// Recovering mode turns each malformed region into an ERROR piece plus one
// diagnostic with the same span, and keeps going.
recovered: Lexer.lex("x: \"open", recover: true)
print(recovered.diagnostics.size())                     // 1
print(recovered.diagnostics[0].code)                    // UNTERMINATED_STRING
errorPiece: recovered.pieces[recovered.pieces.size() - 2]
print(errorPiece.kind)                                  // ERROR
print(errorPiece.rawText)                               // "open