source/lexer/module.protos

Repository
guillermomolina/protos
Commit
379c6fa63685c39a5954cfbd577bfda3b4df8e75
Tree
8cd4f4afe29ae4c3866381f38c1858108e9799f6
Blob
0a43727f6ff26cdfd840a46c31c52e1d2e6b7c75
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 lexical recognition of Protos source text.
//!
//! The lexer partitions a source String into pieces: the Core grammar tokens
//! plus HORIZONTAL_WHITESPACE, LINE_COMMENT, BLOCK_COMMENT and ERROR tool
//! pieces. Positions are zero-based half-open Unicode scalar offsets, and the
//! concatenated `rawText` of all pieces reproduces the source exactly. The
//! source is never parsed, evaluated or executed, and recognition requires no
//! I/O authority.

Scanner: import("../internal/Scanner.protos")
Errors: import("../internal/Errors.protos")

/// Prototype of every lexical failure signalled by strict lexing. Each
/// occurrence is fresh and carries `code`, `start` and `end`.
this.LexicalError: Errors.LexicalError

/// Prototype of every resource-limit failure. Each occurrence is fresh and
/// carries `limit` (`"maxScalars"` or `"maxPieces"`) and `maximum`.
this.LexerLimitError: Errors.LexerLimitError

/// Answers `{ pieces, diagnostics }` for the String `source`.
///
/// Each piece is a fresh record `{ kind, start, end, rawText, lexeme,
/// escapeSpans }`. The lexeme is the spelling for ordinary tokens and NUMBER,
/// the decoded value for STRING, `"\n"` for NEWLINE, `""` for EOF, and null for
/// trivia and ERROR pieces. `escapeSpans` holds `{ start, end }` records for
/// the escapes of a STRING and is empty for every other piece. Exactly one EOF
/// piece ends the Array.
///
/// With `recover: false` the first malformed region signals a fresh
/// LexicalError. With `recover: true` it becomes an ERROR piece plus a
/// `{ code, start, end }` diagnostic, and recognition continues.
///
/// `recover` must be a Boolean, `maxScalars` an Integer of at least zero and
/// `maxPieces` an Integer of at least one; otherwise an Error is signalled.
/// A source longer than `maxScalars` scalars, or a partition needing more than
/// `maxPieces` pieces including EOF, signals a fresh LexerLimitError and
/// answers no partial result.
this.lex: (source, recover: false, maxScalars: 262144, maxPieces: 262145) => {
    String.recognizes(source).ifFalse(() => {
        Errors.signalArgument()
    })
    ((recover === true) || (recover === false)).ifFalse(() => {
        Errors.signalArgument()
    })
    Integer.recognizes(maxScalars).ifFalse(() => {
        Errors.signalArgument()
    })
    (maxScalars >= 0).ifFalse(() => {
        Errors.signalArgument()
    })
    Integer.recognizes(maxPieces).ifFalse(() => {
        Errors.signalArgument()
    })
    (maxPieces >= 1).ifFalse(() => {
        Errors.signalArgument()
    })
    (source.size() <= maxScalars).ifFalse(() => {
        Errors.signalLimit("maxScalars", maxScalars)
    })

    Scanner.scan(source, recover, maxPieces)
}