parser

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 11 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Evie Parser

The parser package implements a hand-written recursive-descent parser with precedence climbing that converts tokens from the internal lexer (internal/lexer) into an ast.Node tree.

Entry Points

  • parser.Parse(input []byte) (ast.Node, error) — parses an anonymous script.
  • parser.ParseSource(src *diag.Source) (ast.Node, error) — parses a named source; syntax errors carry full diag.Report spans and notes pointing at the source text.
  • parser.PeekPackageName(src *diag.Source) string — scans only the leading package <name> header without parsing the full file (used by the multi-file project loader).

Architecture

Every top-level file with a package header parses into an ast.Package; bare scripts parse into an ast.Block.

  • Core driver: parse(precedenceLevel int, asExpr bool) ast.Node calls parseNode, which parses one primary, prefix or keyword construct, then parseInfixExpression, which climbs postfix forms and binary operators for as long as the next operator meets precedenceLevel.
  • Lists: every comma-separated list (imports, parameters, arguments, objects, arrays) goes through ps.list, which allows a trailing comma.
  • Line breaks: a ( or [ that starts a line begins a new statement instead of calling or indexing the line before; . still continues it.
  • Statement vs. expression boundary: statements (if, while, for, switch, declarations, assignments) are rejected in value-expression positions (mustBeValue), while arrow bodies permit assignments and echo.
  • Recursion guard: nested constructs and else if chains track recursion depth against maxParseDepth (256), returning a clean syntax error on deeply nested pathological input instead of overflowing the Go stack. The lexer, which reads template literals ahead of the parser, stops at a deeper nesting of its own (maxTemplateDepth), so the parser's error is the one reported.
  • Panic-free boundary: internal syntax errors abort parsing via fail(...) and are caught by ParseSource's defer recover(), returning *parser.Error (which embeds diag.Report with source excerpts and opening-delimiter notes).

Operator Precedence

From lowest (0) to highest (5):

Level Operators
0 ||
1 &&
2 ==, !=, <, <=, >, >=, is
3 +, -
4 *, /, %
5 Unary +, -, !, await, go
Postfix Calls f(...), member access x.y, existence x.y?, indexing x[i], slicing x[a:b]

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parse

func Parse(input []byte) (ast.Node, error)

Parse parses a script whose source has no name

Example
package main

import (
	"fmt"

	"github.com/hxkhan/evie/ast"
	"github.com/hxkhan/evie/parser"
)

func main() {
	node, err := parser.Parse([]byte("package main\nfn add(a, b) => a + b"))
	if err != nil {
		panic(err)
	}
	pkg := node.(ast.Package)
	fmt.Printf("package=%s decls=%d\n", pkg.Name, len(pkg.Code))
}
Output:
package=main decls=1

func ParseSource

func ParseSource(src *diag.Source) (node ast.Node, err error)

ParseSource parses a script; errors name the source and show its text. Parsing goes on after a syntax error, so a source with several mistakes reports them all (up to ten) as an ErrorList; a single one is an *Error. With errors, the returned node holds the statements that did parse.

func PeekPackageName

func PeekPackageName(src *diag.Source) string

PeekPackageName scans only the leading tokens of src to read the declared package name without parsing the rest of the file. It returns "" for bare scripts or when the header does not start with a valid `package <name>`.

Types

type Error

type Error struct {
	diag.Report
}

Error is a syntax error: a report with the offending span and, where it helps, the construct that was left open

func (*Error) Error

func (e *Error) Error() string

Error returns the report prefixed with "syntax error: "

type ErrorList

type ErrorList []*Error

ErrorList is what parsing returns when a source has more than one syntax error, in the order they appear. errors.As finds each *Error in it.

func (ErrorList) Diagnostics

func (l ErrorList) Diagnostics() []*diag.Report

Diagnostics returns the report of every error, for tools that show them all

func (ErrorList) Error

func (l ErrorList) Error() string

Error returns the first error, and how many more there are

func (ErrorList) Unwrap

func (l ErrorList) Unwrap() []error

Unwrap returns every error in the list, for errors.Is and errors.As

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL