syntax

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package syntax is an idiomatic Go port of @fluent/syntax (Project Fluent): a parser, serializer, and AST visitor for the Fluent localization format.

The AST node types live in the sub-package github.com/hakastein/gofluent/syntax/ast.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ColumnOffset

func ColumnOffset(source string, pos int) int

ColumnOffset returns the zero-based column of pos within its line in source. As with LineOffset, pos is a UTF-16 code-unit offset and the scan is over UTF-16 units, mirroring the reference columnOffset (including its off-by-one: it searches backwards from pos-1 so a line break sitting exactly at pos is not treated as the previous one).

func LineOffset

func LineOffset(source string, pos int) int

LineOffset returns the zero-based line index of pos within source. pos is a UTF-16 code-unit offset, matching the offsets the parser records in spans (the parser is a port of fluent.js, whose stream indexes UTF-16 strings). This mirrors the reference lineOffset, which operates on JS (UTF-16) strings.

func Parse

func Parse(source string, opts ...Option) *ast.Resource

Parse performs a full parse of source with junk recovery. It never returns an error: invalid entries become ast.Junk carrying ast.Annotation diagnostics. Spans are recorded by default; pass WithSpans(false) to disable them.

func ParseEntry

func ParseEntry(source string, opts ...Option) (ast.Entry, error)

ParseEntry parses the first Message or Term in source, skipping any preceding comments. It returns ast.Junk (and a nil error) for unparseable input. The error return mirrors the reference parseEntry signature and is reserved for future use; it is always nil today.

func Serialize

func Serialize(resource *ast.Resource, opts ...SerializerOption) string

Serialize renders a resource to canonical Fluent source. Junk is omitted unless WithJunk(true) is supplied.

func SerializeExpression

func SerializeExpression(expr ast.Expression) string

SerializeExpression renders a single expression to Fluent source.

func SerializeVariantKey

func SerializeVariantKey(key ast.VariantKey) string

SerializeVariantKey renders a variant key (Identifier or NumberLiteral).

func Transform

func Transform(t Transformer, node ast.Node) ast.Node

Transform applies t to node and its descendants, rewriting children in place. It mirrors Transformer.visit + genericVisit: t is applied to node, and if the result is non-nil its children are recursively transformed.

func Walk

func Walk(v Visitor, node ast.Node)

Walk visits node with v, then (if v returned true) descends into each child node in declaration order. It corresponds to Visitor.visit + genericVisit.

Types

type FluentParser

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

FluentParser is a recursive-descent parser for Fluent, a faithful port of parser.ts. Construct one with NewFluentParser, or use the package-level Parse / ParseEntry helpers.

func NewFluentParser

func NewFluentParser(opts ...Option) *FluentParser

NewFluentParser builds a parser with the given options. Spans are enabled by default.

func (*FluentParser) Parse

func (p *FluentParser) Parse(source string) *ast.Resource

Parse performs a full parse with junk recovery. It never returns an error; invalid entries become Junk carrying Annotations.

func (*FluentParser) ParseEntry

func (p *FluentParser) ParseEntry(source string) (ast.Entry, error)

ParseEntry parses the first Message or Term in source, skipping preceding comments. Junk is returned (with no error) for an unparseable entry; only an internal invariant violation would surface as an error, which never happens here, so the error is always nil for malformed input.

type FluentSerializer

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

FluentSerializer renders an AST back to canonical Fluent source. It is a port of FluentSerializer from serializer.ts.

func NewFluentSerializer

func NewFluentSerializer(opts ...SerializerOption) *FluentSerializer

NewFluentSerializer builds a serializer. Junk is omitted by default.

func (*FluentSerializer) Serialize

func (s *FluentSerializer) Serialize(resource *ast.Resource) string

Serialize renders a whole resource.

type Option

type Option func(*FluentParser)

Option configures a FluentParser.

func WithSpans

func WithSpans(enabled bool) Option

WithSpans enables or disables span recording. The default is true, matching FluentParser's default in @fluent/syntax.

type ParseError

type ParseError struct {
	Code    string
	Args    []interface{}
	Message string
}

ParseError is a recoverable parse error carrying a Fluent error code (E0001..) and the formatted human-readable message. During a full Parse these are not returned to the caller; instead they become Annotations on Junk entries.

func (*ParseError) Error

func (e *ParseError) Error() string

Error implements the error interface.

type SerializerOption

type SerializerOption func(*FluentSerializer)

SerializerOption configures a FluentSerializer.

func WithJunk

func WithJunk(enabled bool) SerializerOption

WithJunk includes Junk entries in the serialized output.

type Transformer

type Transformer interface {
	// Transform returns the replacement for node (possibly node itself), or nil
	// to remove it.
	Transform(node ast.Node) ast.Node
}

Transformer is a read-and-write visitor, a port of Transformer from visitor.ts. Transform is called for each node; the returned node replaces the original in the tree, and a nil return removes it. Return the node unchanged to keep it.

type Visitor

type Visitor interface {
	// Visit is called for a node. Return true to let Walk descend into the
	// node's children automatically.
	Visit(node ast.Node) (descend bool)
}

Visitor is a read-only AST visitor, a port of the Visitor base class from visitor.ts. Implementations receive each visited node via Visit. Returning true descends into the node's children via the generic walk; returning false stops descent for that subtree (the implementation is then responsible for any custom traversal, e.g. by calling Walk on selected children).

This is the idiomatic Go shape of the reference's "add a visit{Type} method, then call genericVisit to descend" pattern: switch on the concrete node type inside Visit and return whether to auto-descend.

Directories

Path Synopsis
Package ast defines the Fluent abstract syntax tree, a faithful port of the data model from @fluent/syntax (ast.ts).
Package ast defines the Fluent abstract syntax tree, a faithful port of the data model from @fluent/syntax (ast.ts).

Jump to

Keyboard shortcuts

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