source/parser/module.protos

Repository
guillermomolina/protos
Commit
379c6fa63685c39a5954cfbd577bfda3b4df8e75
Tree
8cd4f4afe29ae4c3866381f38c1858108e9799f6
Blob
c467c2367bd35036e52765e9fca3fc8d9c1d7a63
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.

//! Strict recognition of Protos source text into its written Surface AST.
//!
//! The parser lexes the source once with the Standard Library lexer and recognizes the
//! complete Core grammar. The result describes the syntax exactly as written:
//! Groups, contextual control forms, numeric spellings, trailing bodies and
//! operator spellings are kept, and nothing is canonicalized, lowered,
//! resolved, evaluated or executed. Positions are zero-based half-open Unicode
//! scalar offsets. Recognition requires no I/O authority.

Lexer: import("std:source/lexer")
ParserErrors: import("../internal/ParserErrors.protos")
ParserEngine: import("../internal/ParserEngine.protos")

/// Prototype of every syntax failure signalled by `parse`. Each occurrence is
/// fresh and carries `code`, `start` and `end`.
this.ParseError: ParserErrors.ParseError

/// Prototype of every structural resource-limit failure. Each occurrence is
/// fresh and carries `limit` (`"maxRecords"` or `"maxDepth"`) and `maximum`.
this.ParserLimitError: ParserErrors.ParserLimitError

/// Answers `{ root, source, pieces }` for the String `source`.
///
/// `root` is the Program record of the written syntax. With
/// `retainSource: true`, `source` is the given String and `pieces` the
/// unaltered Array answered by the lexer; otherwise both are null.
///
/// `retainSource` must be a Boolean; `maxScalars` an Integer of at least
/// zero; `maxPieces`, `maxRecords` and `maxDepth` Integers of at least one.
/// Any other argument signals an Error. Lexing is strict, so LexicalError and
/// LexerLimitError from the lexer propagate unchanged. A syntax failure
/// signals a fresh ParseError; a tree needing more than `maxRecords` records
/// or nesting deeper than `maxDepth` records signals a fresh
/// ParserLimitError. No partial result is ever answered.
this.parse: (
    source,
    retainSource: false,
    maxScalars: 262144,
    maxPieces: 262145,
    maxRecords: 524290,
    maxDepth: 1024
) => {
    String.recognizes(source).ifFalse(() => {
        ParserErrors.signalArgument()
    })
    ((retainSource === true) || (retainSource === false)).ifFalse(() => {
        ParserErrors.signalArgument()
    })
    for(bound in [maxScalars, maxPieces, maxRecords, maxDepth]) {
        Integer.recognizes(bound).ifFalse(() => {
            ParserErrors.signalArgument()
        })
    }
    (maxScalars >= 0).ifFalse(() => {
        ParserErrors.signalArgument()
    })
    ((maxPieces >= 1) && (maxRecords >= 1) && (maxDepth >= 1)).ifFalse(() => {
        ParserErrors.signalArgument()
    })

    lexed: Lexer.lex(
        source,
        recover: false,
        maxScalars: maxScalars,
        maxPieces: maxPieces
    )
    tree: ParserEngine.parse(lexed.pieces, source.size(), maxRecords, maxDepth)

    keptSource: null
    keptPieces: null
    if(retainSource) {
        keptSource = source
        keptPieces = lexed.pieces
    }
    {
        root: tree
        source: keptSource
        pieces: keptPieces
    }
}