svparse

module
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2026 License: BSD-3-Clause

README

svparse

An error-tolerant SystemVerilog/Verilog parser library for Go, built for language-server-style tooling: it never panics or stops on malformed or mid-edit source.

Capabilities

svparse is declaration-grade, not a full expression/statement parser:

  • Parsed for real: ports, nets/variables, parameters, typedef/struct/enum, function/task signatures, class members, package imports, containers (module/interface/program), and instantiations.
  • Intentionally not parsed: expression and statement grammar, and procedural block bodies (always, function/task bodies, constraint blocks). These are recognized and skipped via balanced-token matching, and kept as raw token spans on the AST rather than an expression tree.

The pipeline is layered across four packages:

Package Role
token Token vocabulary (Token{Kind, Text, Line, Character})
lexer Scans source into a token stream
preprocessor Macro expansion, conditional compilation, `include resolution, with source maps back to real positions
parser Builds the ast.File declaration tree

Install

go get github.com/jfetkotto/svparse

Usage

import (
    "github.com/jfetkotto/svparse/parser"
    "github.com/jfetkotto/svparse/preprocessor"
)

toks, ppErrs := preprocessor.Preprocess(path, text, resolver)
file, parseErrs := parser.Parse(path, toks)
  • lexer.Lex(src string) ([]token.Token, []Error): tokenize only.
  • preprocessor.Preprocess(path, text string, resolver IncludeResolver) ([]Token, []Error): tokenize, expand macros, and resolve `includes. PreprocessWithOptions takes an Options struct for finer control.
  • parser.Parse(path string, toks []preprocessor.Token) (*ast.File, []Error): parse preprocessed tokens into an ast.File.
  • parser.ParseTokens(path string, toks []token.Token) (*ast.File, []Error): parse raw lexer tokens directly, skipping preprocessing.

None of these functions ever panic; malformed input is recorded in the returned []Error and parsing/lexing continues.

Testing

go test ./...

This runs unit and integration tests for every package, fuzz tests asserting the lexer/preprocessor/parser never panic on arbitrary input, and a corpus test (parser/corpus_test.go) that runs the parser over a vendored real-world SystemVerilog corpus under testdata/sv-tests/ against a checked-in baseline.

License

BSD 3-Clause. See LICENSE.

Directories

Path Synopsis
Package ast holds the declaration-grade AST node types svparse/parser produces.
Package ast holds the declaration-grade AST node types svparse/parser produces.
Package lexer scans SystemVerilog/Verilog source text into a token stream (see the sibling token package for the vocabulary).
Package lexer scans SystemVerilog/Verilog source text into a token stream (see the sibling token package for the vocabulary).
Package parser implements an error-tolerant, declaration-grade recursive-descent parser for SystemVerilog, producing an svparse/ast tree from a token stream.
Package parser implements an error-tolerant, declaration-grade recursive-descent parser for SystemVerilog, producing an svparse/ast tree from a token stream.
Package preprocessor implements SV compiler-directive handling on top of svparse/lexer's token stream: macro definition and expansion, conditional compilation, and “ `include “ resolution, all with source maps so an expanded or included token still resolves back to a real, editor-true position rather than "somewhere inside a macro definition" or "the position “ `include “ itself sat at".
Package preprocessor implements SV compiler-directive handling on top of svparse/lexer's token stream: macro definition and expansion, conditional compilation, and “ `include “ resolution, all with source maps so an expanded or included token still resolves back to a real, editor-true position rather than "somewhere inside a macro definition" or "the position “ `include “ itself sat at".
Package token holds the lexer's token kinds and shared position helpers.
Package token holds the lexer's token kinds and shared position helpers.

Jump to

Keyboard shortcuts

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