source

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: 2 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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func JoinMaps

func JoinMaps[S comparable, T comparable](target *Map[S], source *Map[T], mapping func(T) S)

JoinMaps incorporates all mappings from one source map (the source) into another source map (the target), whilst applying a given mapping to the node types.

Types

type File

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

File represents a given source file (typically stored on disk).

func NewSourceFile

func NewSourceFile(filename string, bytes []byte) *File

NewSourceFile constructs a new source file from a given byte array.

func ReadFiles

func ReadFiles(filenames ...string) ([]File, error)

ReadFiles reads a given set of source files, or produces an error.

func (*File) Contents

func (s *File) Contents() []rune

Contents returns the contents of this source file.

func (*File) Filename

func (s *File) Filename() string

Filename returns the filename associated with this source file.

func (*File) FindFirstEnclosingLine

func (s *File) FindFirstEnclosingLine(span Span) Line

FindFirstEnclosingLine determines the first line in this source file which encloses the start of a span. Observe that, if the position is beyond the bounds of the source file then the last physical line is returned. Also, the returned line is not guaranteed to enclose the entire span, as these can cross multiple lines.

func (*File) Lines

func (s *File) Lines() []Line

Lines splits the contents of this file into individual lines.

func (*File) SyntaxError

func (s *File) SyntaxError(span Span, msg string) *SyntaxError

SyntaxError constructs a syntax error over a given span of this file with a given message.

type Line

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

Line provides information about a given line within the original string. This includes the line number (counting from 1), and the span of the line within the original string.

func (*Line) Length

func (p *Line) Length() int

Length returns the number of characters in this line.

func (*Line) Number

func (p *Line) Number() int

Number gets the line number of this line, where the first line in a string has line number 1.

func (*Line) Start

func (p *Line) Start() int

Start returns the starting index of this line in the original string.

func (*Line) String

func (p *Line) String() string

String returns a string representing this line.

type Map

type Map[T comparable] struct {
	// contains filtered or unexported fields
}

Map maps terms from an AST to slices of their originating string. This is important for error handling when we wish to highlight exactly where, in the original source file, a given error has arisen.

This provides various useful functions to aid reporting syntax errors, such as identifying the enclosing line for a given span, etc.

func NewSourceMap

func NewSourceMap[T comparable](srcfile File) *Map[T]

NewSourceMap constructs an initially empty source map for a given string.

func (*Map[T]) Get

func (p *Map[T]) Get(item T) Span

Get determines the span associated with a given AST item extract from the original text. Note, if the item is not registered with this source map, then it will panic.

func (*Map[T]) Has

func (p *Map[T]) Has(item T) bool

Has checks whether a given item is contained within this source map.

func (*Map[T]) Put

func (p *Map[T]) Put(item T, span Span)

Put registers a new AST item with a given span. Note, if the item exists already, then it will panic.

func (*Map[T]) Source

func (p *Map[T]) Source() File

Source returns the underlying source file on which this map operates.

func (*Map[T]) SyntaxError

func (p *Map[T]) SyntaxError(item T, msg string) *SyntaxError

SyntaxError constructs a syntax error for a given node contained within the source file associated with this source map.

func (*Map[T]) SyntaxErrors

func (p *Map[T]) SyntaxErrors(item T, msg string) []SyntaxError

SyntaxErrors is really just a helper that construct a syntax error and then places it into an array of size one. This is helpful for situations where sets of syntax errors are being passed around.

type Maps

type Maps[T comparable] struct {
	// contains filtered or unexported fields
}

Maps provides a mechanism for mapping terms from an AST to multiple source files.

func NewSourceMaps

func NewSourceMaps[T comparable]() *Maps[T]

NewSourceMaps constructs an (initially empty) set of source maps. The intention is that this is populated as each file is parsed.

func (*Maps[T]) Copy

func (p *Maps[T]) Copy(from T, to T)

Copy copies the source mapping for one node to the source mapping for another. The main use of this is when an existing node is expanded into some other nodes (e.g. during preprocessing).

func (*Maps[T]) Has

func (p *Maps[T]) Has(node T) bool

Has checks whether a given node has a mapping in one of the source maps embodied within.

func (*Maps[T]) Join

func (p *Maps[T]) Join(srcmap *Map[T])

Join a given source map into this set of source maps. The effect of this is that nodes recorded in the given source map can be accessed from this set.

func (*Maps[T]) Lookup

func (p *Maps[T]) Lookup(node T) (File, Span, bool)

Lookup returns the source file and span for a given node. If the node is found in any of the contained source maps, it returns the corresponding File and Span with ok=true; otherwise it returns zero values with ok=false.

func (*Maps[T]) SyntaxError

func (p *Maps[T]) SyntaxError(node T, msg string) *SyntaxError

SyntaxError constructs a syntax error for a given node contained within one of the source files managed by this set of source maps.

func (*Maps[T]) SyntaxErrors

func (p *Maps[T]) SyntaxErrors(node T, msg string) []SyntaxError

SyntaxErrors is really just a helper that construct a syntax error and then places it into an array of size one. This is helpful for situations where sets of syntax errors are being passed around.

type Span

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

Span represents a contiguous slice of the original string. Instead of representing this as a string slice, however, it is useful to retain the physical indices. This allows us to do certain things, such as determine the enclosing line, etc.

func NewSpan

func NewSpan(start int, end int) Span

NewSpan constructs a new span whilst checking the internal invariants are maintained.

func (*Span) End

func (p *Span) End() int

End returns one past the last index of this span in the original string.

func (*Span) Length

func (p *Span) Length() int

Length returns the number of characters covered by this span in the original string.

func (*Span) Start

func (p *Span) Start() int

Start returns the starting index of this span in the original string.

type SyntaxError

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

SyntaxError is a structured error which retains the index into the original string where an error occurred, along with an error message.

func (*SyntaxError) Error

func (p *SyntaxError) Error() string

Error implements the error interface.

func (*SyntaxError) FirstEnclosingLine

func (p *SyntaxError) FirstEnclosingLine() Line

FirstEnclosingLine determines the first line in this source file to which this error is associated. Observe that, if the position is beyond the bounds of the source file then the last physical line is returned. Also, the returned line is not guaranteed to enclose the entire span, as these can cross multiple lines.

func (*SyntaxError) Message

func (p *SyntaxError) Message() string

Message returns the message to be reported.

func (*SyntaxError) SourceFile

func (p *SyntaxError) SourceFile() *File

SourceFile returns the underlying source file that this syntax error covers.

func (*SyntaxError) Span

func (p *SyntaxError) Span() Span

Span returns the span of the original text on which this error is reported.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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