Sign in

    moonrockz/gleam-parser/parser does not have a README file

    ParseError

    pub(all) suberror ParseError {
    ParseError(error~ : ParseErrorType, location~ :
    SrcSpan
    )
    } derive(Eq,
    Debug
    )

    A syntax error: the compiler's error and its span.

    ParseError::to_diagnostic

    The diagnostic for a parse error: GL-PARSE-001 with the compiler's title "Syntax error" and wording; GL-PARSE-004 for TooMuchNesting; the scanner's own diagnostic for LexError. UnexpectedEof keeps the compiler's raw span 0..0; the renderer moves it to the last byte as error.rs does.

    IncorrectNamePosition

    pub(all) enum IncorrectNamePosition {
    AsPattern
    Module
    Function
    Attribute
    Constant
    Target
    Variable
    } derive(Eq,
    Debug
    )

    Where an incorrect name was found (the compiler's IncorrectNamePosition).

    ParseErrorDetails

    pub(all) struct ParseErrorDetails {
    text : String
    label_text : String
    extra_labels : ArrayView[
    Label
    ]
    hint : String?
    } derive(Eq,
    Debug
    )

    The compiler's ParseErrorDetails: the body text, the primary label text, the extra labels and the hint.

    ParseErrorType

    pub(all) enum ParseErrorType {
    ExpectedEqual
    ExpectedExpr
    ExpectedName
    ExpectedPattern
    ExpectedType
    ExpectedUpName
    ExpectedValue
    ExpectedDefinition
    ExpectedDeprecationMessage
    ExpectedExternalArguments
    ExpectedFunctionDefinition
    ExpectedTargetName
    ExprLparStart
    ExtraSeparator
    IncorrectName(IncorrectNamePosition)
    IncorrectUpName
    InvalidBitArraySegment
    InvalidBitArrayUnit
    InvalidTailPattern
    InvalidTupleAccess
    LexError(
    Diagnostic
    )
    NestedBitArrayPattern
    NoLetBinding
    NoValueAfterEqual
    NotConstType
    OpNakedRight
    OpaqueTypeAlias
    TooManyArgHoles
    DuplicateAttribute
    UnknownAttribute
    UnknownTarget
    ListSpreadWithoutElements
    ListSpreadFollowedByElements
    ListSpreadWithAnotherSpread(first_spread_location~ :
    SrcSpan
    )
    UnexpectedLabel
    UnexpectedEof
    UnexpectedReservedWord
    UnexpectedToken(token~ :
    TokenKind
    , expected~ : ArrayView[String], hint~ : String?)
    UnexpectedFunction
    ConcatPatternVariableLeftHandSide
    ConcatPatternVariableWithSuffix(name~ : String)
    ListSpreadWithoutTail
    ExpectedFunctionBody
    RedundantInternalAttribute
    InvalidModuleTypePattern
    ListPatternSpreadFollowedByElements
    ExpectedRecordConstructor(type_name~ : String, public~ : Bool, opaque_~ : Bool, fields~ : ArrayView[
    RecordConstructorArg
    ])
    CallInClauseGuard
    IfExpression
    TypeDefinitionNoArguments
    UnknownAttributeRecordVariant
    IncorrectImportModuleSeparator(module_~ : String, item~ : String)
    EmptyGuardBlock
    ConstantInsideFunction
    FunctionDefinitionAngleGenerics
    TypeUsageAngleGenerics(module_~ : String?, name~ : String, arguments~ : ArrayView[
    TypeAst
    ])
    TypeDefinitionAngleGenerics(name~ : String, arguments~ : ArrayView[String])
    MissingConstantAsMessage
    RecordUpdateAfterFields
    TooMuchNesting
    } derive(Eq,
    Debug
    )

    The compiler's ParseErrorType, one variant each, plus TooMuchNesting (gleam-parser's own limit). LexError carries the scanner's diagnostic for a lexical error met by a snippet parser.

    ParseErrorType::details

    The compiler's ParseErrorType::details(): body text, primary label, extra labels and hint, with its exact wording and wrap at 75 bytes.

    ParseErrorType::kind

    fn ParseErrorType::kind(self : ParseErrorType) -> String

    The variant name (UnexpectedToken, …), as the oracle writes it.

    ParseResult

    What parse_module returns: the AST (module_), the compiler's extra, the warnings, the CST (every token, grouped per definition, also the skipped ones) and the diagnostics. A definition with a syntax error is not in module_; its tokens are a Skipped run in cst.

    test {
    let r = @parser.parse_text("pub fn main() {\n 1\n}\n")
    inspect(r.module_.definitions.length(), content="1")
    inspect(r.has_errors(), content="false")
    inspect(r.cst.to_source(), content="pub fn main() {\n 1\n}\n")
    }

    ParseResult::has_errors

    fn ParseResult::has_errors(self : ParseResult) -> Bool

    Whether any diagnostic is an error.

    ParseResult::parsed

    The oracle's Parsed shape: module, extra and warnings.

    definition_root_depth

    let definition_root_depth : Int

    The JSON path depth of $.module.definitions[i].definition: the steps the decoder counts before a definition's own fields.

    json_depth

    The depth of the deepest JSON path inside a definition's canonical JSON, counted as the decoder counts (@ast.max_depth): steps from the definition object to its deepest scalar. Measured with an explicit stack, so a definition of any depth is safe to measure.

    test {
    let r = @parser.parse_text("const x = 1\n")
    // $.location.start, $.name_location.start, $.value.location.start: 3
    inspect(@parser.json_depth(r.module_.definitions[0].definition), content="3")
    }

    max_nesting

    let max_nesting : Int

    Deepest syntactic nesting the parser enters (brackets, blocks, case, fn, nested patterns, types and constants). Deeper text is a GL-PARSE-004 error (TooMuchNesting), never a stack overflow, on every target: wasm overflows at about 140 nested lists, so the limit is 100. The JSON depth limit (@ast.max_depth) rejects most constructs before this one. The Gleam compiler has no such limit.

    parse_constant_text

    fn parse_constant_text(text : String) ->
    Constant
    raise ParseError

    Parse text as one constant value (the compiler's parse_const_value test interface; a missing value is ExpectedExpr at 0..0).

    parse_expression_text

    fn parse_expression_text(text : String) ->
    Expr
    raise ParseError

    Parse text as one expression (a pipeline or an operator chain counts as one); a missing expression is ExpectedExpr at 0..0.

    test {
    let e = @parser.parse_expression_text("1 + 2")
    debug_inspect(e.location(), content="{ start: 0, end: 5 }")
    }

    parse_module

    Parse a token stream into a module. Recovery per definition: an error is reported, the parser skips to the next definition start (import, const, pub, fn, type, opaque or @ in column 1) and goes on. The first error diagnostic has the compiler's kind and span.

    parse_pattern_text

    fn parse_pattern_text(text : String) ->
    Pattern
    raise ParseError

    Parse text as one pattern.

    parse_statements_text

    fn parse_statements_text(text : String) -> ArrayView[
    Statement
    ] raise ParseError

    Parse text as a statement sequence (a function body without braces), the compiler's parse_statement_sequence test interface.

    parse_text

    fn parse_text(text : String, dialect? :
    Dialect
    ) -> ParseResult

    parse_module of the tokens of text.

    parse_type_text

    fn parse_type_text(text : String) ->
    TypeAst
    raise ParseError

    Parse text as one type annotation (without :), for tests and tools.

    test {
    let t = @parser.parse_type_text("List(a)")
    debug_inspect(t.location(), content="{ start: 0, end: 7 }")
    }

    token_text

    The compiler's Display for a token: its text in backquotes. A name, number or string shows its own text.

    type_ast_text

    fn type_ast_text(t :
    TypeAst
    ) -> String

    The source form of a type annotation (TypeAst::print), for messages.

    warning_diagnostic

    The diagnostic for a parser warning, with the compiler's title, text and label (warning.rs): GL-PARSE-002 for deprecated syntax, GL-PARSE-003 for a detached doc comment.