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
- func Serialize(resource *ast.Resource, opts ...SerializerOption) string
- func Transform(t Transformer, node ast.Node) ast.Node
- func Walk(v Visitor, node ast.Node)
- type Option
- type ParseError
- type Parser
- type Serializer
- 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 for unparseable input.
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.
Types ¶
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
type Parser ¶ added in v0.4.0
type Parser struct {
// contains filtered or unexported fields
}
Parser is a recursive-descent parser for Fluent, a faithful port of the @fluent/syntax FluentParser. Construct one with NewParser, or use the package-level Parse / ParseEntry helpers.
func NewParser ¶ added in v0.4.0
NewParser builds a parser with the given options. Spans are enabled by default.
type Serializer ¶ added in v0.4.0
type Serializer struct {
// contains filtered or unexported fields
}
Serializer renders an AST back to canonical Fluent source. It is a port of the @fluent/syntax FluentSerializer.
func NewSerializer ¶ added in v0.4.0
func NewSerializer(opts ...SerializerOption) *Serializer
NewSerializer builds a serializer. Junk is omitted by default.
type SerializerOption ¶
type SerializerOption func(*Serializer)
SerializerOption configures a Serializer.
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.