parser

package
v1.2.23 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Copyright Consensys Software Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

SPDX-License-Identifier: Apache-2.0

Copyright Consensys Software Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

SPDX-License-Identifier: Apache-2.0

Copyright Consensys Software Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

SPDX-License-Identifier: Apache-2.0

Index

Constants

View Source
const (
	// EOF signals "end of file"
	EOF uint = iota
	// WHITESPACE signals whitespace
	WHITESPACE
	// NEWLINE signals a newline character
	NEWLINE
	// COMMENT signals "// ... \n"
	COMMENT
	// LBRACE signals "("
	LBRACE
	// RBRACE signals ")"
	RBRACE
	// LCURLY signals "{"
	LCURLY
	// RCURLY signals "}"
	RCURLY
	// LSQUARE signals "["
	LSQUARE
	// RSQUARE signals "]"
	RSQUARE
	// COMMA signals ","
	COMMA
	// COLON signals ":"
	COLON
	// COLONCOLON signals "::"
	COLONCOLON
	// SEMICOLON signals ";"
	SEMICOLON
	// NUMBER signals an integer number
	NUMBER
	// STRING signals a quoted string
	STRING
	// IDENTIFIER signals a column variable
	IDENTIFIER
	// KEYWORD_AS signals a type cast expression (e.g. "x as u8")
	KEYWORD_AS
	// KEYWORD_BREAK signals a break statement
	KEYWORD_BREAK
	// KEYWORD_CONTINUE signals a continue statement
	KEYWORD_CONTINUE
	// KEYWORD_CONST signals a constant declaration
	KEYWORD_CONST
	// KEYWORD_ELSE signals an else branch
	KEYWORD_ELSE
	// KEYWORD_FAIL signals a return statement
	KEYWORD_FAIL
	// KEYWORD_FN signals a function declaration
	KEYWORD_FN
	// KEYWORD_FOR signals a for loop
	KEYWORD_FOR
	// KEYWORD_IF signals a return statement
	KEYWORD_IF
	// KEYWORD_INCLUDE signals an include declaration
	KEYWORD_INCLUDE
	// KEYWORD_INPUT signals a read-only memory
	KEYWORD_INPUT
	// KEYWORD_MEMORY signals a random-access memory declaration
	KEYWORD_MEMORY
	// KEYWORD_RETURN signals a return statement
	KEYWORD_RETURN
	// KEYWORD_STATIC signals a static read-only memory
	KEYWORD_STATIC
	// KEYWORD_OUTPUT signals a write-once memory
	KEYWORD_OUTPUT
	// KEYWORD_PRINTF signals a printf statement
	KEYWORD_PRINTF
	// KEYWORD_PUB signals a public input / output
	KEYWORD_PUB
	// KEYWORD_WHILE signals a while loop
	KEYWORD_WHILE
	// KEYWORD_VAR signals a local variable declaration
	KEYWORD_VAR
	// KEYWORD_TYPE signals a type alias declaration
	KEYWORD_TYPE
	// KEYWORD_SWITCH signals the beginning of a switch statement
	KEYWORD_SWITCH
	// KEYWORD_CASE signals a case in a switch statement
	KEYWORD_CASE
	// KEYWORD_DEFAULT signals the default case in a switch statement
	KEYWORD_DEFAULT
	// RIGHTARROW signals "->"
	RIGHTARROW
	// EQUALS signals "="
	EQUALS
	// EQUALS_EQUALS signals "=="
	EQUALS_EQUALS
	// NOT_EQUALS signals "!="
	NOT_EQUALS
	// LESS_THAN signals "<"
	LESS_THAN
	// LESS_THAN_EQUALS signals "<="
	LESS_THAN_EQUALS
	// GREATER_THAN signals ">"
	GREATER_THAN
	// GREATER_THAN_EQUALS signals ">="
	GREATER_THAN_EQUALS
	// LOGICAL_AND signals "&&"
	LOGICAL_AND
	// LOGICAL_OR signals "||"
	LOGICAL_OR
	// LOGICAL_NOT signals "!"
	LOGICAL_NOT
	// ADD signals "+"
	ADD
	// SUB signals "-"
	SUB
	// MUL signals "*"
	MUL
	// DIV signals "/"
	DIV
	// BITWISE_AND signals "&"
	BITWISE_AND
	// BITWISE_OR signals "|"
	BITWISE_OR
	// BITWISE_XOR signals "^"
	BITWISE_XOR
	// BITWISE_NOT signals "~"
	BITWISE_NOT
	// BITWISE_SHL signals "<<"
	BITWISE_SHL
	// BITWISE_SHR signals ">>"
	BITWISE_SHR
	// REM signals "%"
	REM
	// QMARK signals "?"
	QMARK
	// HASH signals "#"
	HASH
	// AT signals "@"
	AT
	// UNKNOWN signals an unknown chunk of text
	UNKNOWN
	// SPACES signal a set of one or more spaces.  This is a virtual token only
	// used by the formatter.
	SPACES
	// TABS signal a set of one or more tabs.  This is a virtual token only used
	// by the formatter.
	TABS
	// FIELD_ELEMENT signals the "𝔽" field element type token.
	FIELD_ELEMENT
	// MAX_TOKEN signals the maximum token index
	MAX_TOKEN
)

Variables

BINOPS captures the set of binary operations

View Source
var LOGICAL_BINOPS = []uint{LOGICAL_AND, LOGICAL_OR}

LOGICAL_BINOPS captures the set of logical binary operations

View Source
var MAX_KEYWORD_LENGTH int

MAX_KEYWORD_LENGTH is used to optimise lexing of keywords.

Functions

func IsValidIdentifier

func IsValidIdentifier(s string) bool

IsValidIdentifier reports whether s parses as a single IDENTIFIER token, rejecting empty strings, keywords, and anything containing whitespace or punctuation.

func Lex

func Lex(srcfile source.File, whitespace, comments bool) []lex.Token

Lex a given source file into a sequence of zero or more tokens, along with any syntax errors arising. This can be configured to retain whitespace and/or comments.

Types

type Condition

type Condition = expr.Condition[symbol.Unresolved]

Condition is a convenient alias

type Environment

type Environment struct {
	// contains filtered or unexported fields
}

Environment captures useful information used during the assembling process.

func EmptyEnvironment

func EmptyEnvironment() Environment

EmptyEnvironment constructs an initially empty environment

func (*Environment) Clone

func (env *Environment) Clone(inLoop bool) Environment

Clone constructs a clone of this environment, such that variables declared in the clone will not clash with those declared elsewhere. The inLoop parameter indicates whether the cloned environment is inside a loop.

func (*Environment) DeclareEffect

func (env *Environment) DeclareEffect(effect *symbol.Unresolved)

DeclareEffect declares a new effect. If an effect with the same name already exists, this panics.

func (*Environment) DeclareVariable

func (env *Environment) DeclareVariable(kind variable.Kind, name string, datatype Type)

DeclareVariable declares a new register with the given name and bitwidth. If a register with the same name already exists, this panics.

Note. DeclareVariable allows one to introduce local variables whose names shadow the names of top level declarations (constants, functions, memories and type aliases). See https://github.com/LFDT-Lineth/zkc/issues/1921.

func (*Environment) Effects

func (env *Environment) Effects() []*symbol.Unresolved

Effects returns the set of memory effects declared globally

func (*Environment) InLoop

func (env *Environment) InLoop() bool

InLoop returns whether the current environment is inside a loop body.

func (*Environment) IsDeclared

func (env *Environment) IsDeclared(name string) bool

IsDeclared checks whether or not a given name is already declared (either as an effect or a variable).

func (*Environment) IsDeclaredVariable

func (env *Environment) IsDeclaredVariable(name string) bool

IsDeclaredVariable checks whether or not a given name is already declared as a variable.

func (*Environment) LookupVariable

func (env *Environment) LookupVariable(name string) variable.Id

LookupVariable looks up the index for a given register.

func (*Environment) Variables

func (env *Environment) Variables() []VariableDescriptor

Variables returns the set of variables declared globally

type Expr

type Expr = expr.Expr[symbol.Unresolved]

Expr is a convenient alias

type LVal

type LVal = lval.LVal[symbol.Unresolved]

LVal is a convenient alias

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser is a parser for assembly language.

func NewParser

func NewParser(srcfile *source.File) *Parser

NewParser constructs a new parser for a given source file.

func (*Parser) Parse

func (p *Parser) Parse() (UnlinkedSourceFile, []source.SyntaxError)

Parse the given source file into a sequence of zero or more components and/or some number of syntax errors.

type Type

type Type = data.Type[symbol.Unresolved]

Type is a convenient alias

type UnlinkedSourceFile

type UnlinkedSourceFile struct {
	// (top-level) Declarations making up this assembly item.
	Declarations []decl.Unresolved
	// Mapping of instructions back to the source file.
	SourceMap source.Map[any]
}

UnlinkedSourceFile captures a source file has been successfully parsed but which has not yet been linked. As such, its possible that such a file may fail with an error at link time due to an unresolvable reference to an external component (e.g. function, RAM, ROM, etc).

func Parse

func Parse(srcfile *source.File) (UnlinkedSourceFile, []source.SyntaxError)

Parse accepts a given source file representing an assembly language program, and assembles it into an instruction sequence which can then the executed.

type VariableDescriptor

type VariableDescriptor = variable.Descriptor[symbol.Unresolved]

VariableDescriptor is a convenient alias

Jump to

Keyboard shortcuts

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