parser

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2026 License: BSD-3-Clause Imports: 7 Imported by: 0 modules, 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.

Documentation

Overview

Package parser generates an Abstract Syntax Tree (AST) from a sequence of tokens, generated by lexer.

CONTEXT FREE GRAMMAR

Goal = Expr | Func | Var

Func = "def" Identifier "(" [ Params ] ")" Expr Identifier = Letter { ( Letter | Digit ) } Letter = "a" .. "z" | "A" .. "Z" Digit = "0" .. "9" Params = Identifier { "," Identifier }

Var = Identifier "=" Expr

Expr = Operand | BinExpr

Operand = Number | Identifier | Group | Call Number = Digit [ "." { Digit } ] | "." Digit Group = "(" Expr ")" Call = Ident "(" [ Args ] ")" Args = Expr { "," Expr }

BinExpr = Expr Op Expr Op = AddOp | MulOp AddOp = "+" | "-" MulOp = "*" | "/"

EXAMPLE

var n = 5

# comment
def fib(x)
	if x < 3 then
		1
	else
		fib(x-1)+fib(x-2)

fib(10)

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrParse = errors.New("parse error")

ErrParse means there is an error in parsing.

Functions

func Parse

func Parse(s string) (ast.Node, error)

Parse parses the code segment s.

func ParseExpr

func ParseExpr(s string) (ast.Node, error)

ParseExpr parses a single expression. A declaration is a valid expression.

Example
package main

import (
	"fmt"

	"github.com/skhal/lab/x/kscope/internal/parser"
)

func main() {
	const s = `
var a = 1
var b = a + 22
def c()
	a + b * 2
def d(x, y)
  x * c() + a
`
	n, err := parser.Parse(s)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(n)
}
Output:
var a = 1.0
var b = a + 22.0
def c()
  a + b * 2.0
def d(x, y)
  x * c() + a

Types

This section is empty.

Jump to

Keyboard shortcuts

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