parser

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: May 18, 2026 License: MIT Imports: 7 Imported by: 0

README

parser

⬆️ Parent Directory

Overview

The parser package handles AST-based normalization of source code into canonical token streams.

Components

normalizer.go

Converts source code into normalized token streams using Go's go/ast package.

Key Functions

NewNormalizer() *Normalizer

  • Creates a new normalizer instance
  • Initializes token.FileSet for AST parsing

NormalizeFile(filePath string) (*types.FileTokens, error)

  • Parses a Go source file into an AST
  • Extracts and normalizes tokens
  • Returns normalized token stream

extractTokens(file *ast.File, src []byte) []types.Token

  • Traverses AST using visitor pattern
  • Normalizes identifiers → _ID_
  • Normalizes literals → _LIT_
  • Preserves keywords and operators
Normalization Rules
Input Output
Identifiers (myVar, calculateSum) _ID_
Literals ("hello", 42, 3.14) _LIT_
Keywords (if, for, return) as-is
Operators (+, ==, :=) as-is
Punctuation ((, ), {) as-is

Example

// Original code
func calculateSum(a int, b int) int {
    result := a + b
    return result
}

// Normalized token stream
func _ID_ ( _ID_ _ID_ , _ID_ _ID_ ) _ID_ { _ID_ := _ID_ + _ID_ return _ID_ }

Performance

  • Throughput: ~30MB/s
  • Bottleneck: CPU-bound (AST traversal)
  • Scalability: Parallelizable per file

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func TokenStreamToString

func TokenStreamToString(tokens []types.Token) string

TokenStreamToString converts a token stream to a string for debugging

Types

type Normalizer

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

Normalizer converts source code into normalized token streams

func NewNormalizer

func NewNormalizer() *Normalizer

NewNormalizer creates a new normalizer instance

func (*Normalizer) NormalizeFile

func (n *Normalizer) NormalizeFile(filePath string) (*types.FileTokens, error)

NormalizeFile parses a Go source file and returns a normalized token stream

Jump to

Keyboard shortcuts

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