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 ¶
- func ColumnOffset(source string, pos int) int
- func LineOffset(source string, pos int) int
- func Parse(source string, opts ...Option) *ast.Resource
- func ParseEntry(source string, opts ...Option) (ast.Entry, error)
- func Serialize(resource *ast.Resource, opts ...SerializerOption) string
- func SerializeExpression(expr ast.Expression) string
- func SerializeVariantKey(key ast.VariantKey) string
- func Transform(t Transformer, node ast.Node) ast.Node
- func Walk(v Visitor, node ast.Node)
- type FluentParser
- type FluentSerializer
- type Option
- type ParseError
- type SerializerOption
- type Transformer
- type Visitor
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ColumnOffset ¶
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 ¶
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 ¶
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 ¶
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).
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.
type ParseError ¶
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.