resrap

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2025 License: GPL-3.0 Imports: 13 Imported by: 0

README

Resrap

Landing Go Reference

Just a parser… in reverse.


What is Resrap?

Resrap is a seedable, grammar-based code snippet generator. Instead of parsing code, it generates code from formal grammars — producing endless, realistic-looking (or hilariously nonsensical) snippets.

It works with any language that can be described with a grammar (even English if you like!) and is perfect for:

  • Typing practice with realistic-looking snippets
  • Stress-testing parsers, syntax highlighters, or linters
  • Fun exploration of procedural code generation

Resrap now also supports probabilistic and infinitely repeatable grammars via the ABNF (Awesome BNF) format — see docs/ABNF.md for full reference.


How?

Resrap reads a grammar and builds a graph of expansions. It then randomly traverses the graph (or deterministically with a seed) to produce snippets that:

  • Follow the grammar’s syntax rules
  • Look structurally like real code
  • Include probabilities for weighted choices (<0.2>)
  • Support infinite loops via the ^ operator

Example grammar snippet (simplified C):

program : (header+<0.4>) function^;
header:'#include<'identifier'.h>\n';
function:functionheader'{''\n'functioncontent'}';
functionheader:datatype ' ' identifier '(' ')' ;
...
...

Generated code example

#include<success.h>
#include<email.h>
double class(){
while(variable < query && password < variable){
int result = variable + (user / hello);
}
}double user(){
if(class > user && result < class){
float password = 1024.13 - (13.7);
}
}double hello(

Installation

go get github.com/ItsArnavSh/Resrap@v0.1.0

Usage


	//Resrap with Single threaded
	rs := resrap.NewResrap()
	err := rs.ParseGrammarFile("C", "example/C.g4")
	if err != nil {
		fmt.Println(err)
		return
	}
	code := rs.GenerateRandom("C", "program", 10)
	fmt.Println(code)

	//Lets get a multithreaded API set up quick
	r := resrap.NewResrapMT(20, 1000) //20 worker pool and 1000 wait queue max size
	err = r.ParseGrammarFile("C", "example/C.g4")
	if err != nil {
		fmt.Println(err)
		return
	}
	//Receive from this
	r.StartResrap()
	defer r.ShutDownResrap()
	codeChan := r.GetCodeChannel()
	id := "12321"
	r.GenerateRandom(id, "C", "program", 10)
	res := <-codeChan
	fmt.Println(res.Code)
Notes
  • IDs: You must create unique IDs for each job; results are returned with the ID.
  • CodeChannel: A blocking, unbounded channel — handle results yourself.
  • Why multithreaded? Efficiently handles many concurrent jobs, fully utilizing CPU cores while keeping grammar graphs immutable and lock-free.

For benchmarks and performance comparisons, see benchmark-results/Multithreading.md.


Roadmap

  • Maintain generation sessions (e.g., generate snippets in chunks)
  • Dynamic worker scaling and idle worker shutdown

Motivation

Resrap was created to:

  • Generate unlimited, realistic code snippets
  • Avoid copyright issues from using real code
  • Provide a fun, deterministic, and probabilistic code generator
  • Give programmers a playground for syntax, speed, and randomness

“Just a parser… in reverse.”


ABNF (Awesome BNF)

  • ^ → Infinite generation (loops nodes without halting)
  • <prob> → Weighted probabilities for branching
  • Compatible with standard EBNF operators: +, *, ?, ()

See docs/ABNF.md for full syntax and examples.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CodeGenRes

type CodeGenRes struct {
	Code string
	Id   string
}

CodeGenRes contains the process id along with the code generated returned from ResrapMT

type Resrap

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

Resrap is the main accesspoint for singlethreaded uses Pretty Much Collection of graphs which can be generated using parsing grammar

func NewResrap

func NewResrap() *Resrap

NewResrap creates and returns a new Resrap instance. The returned instance starts with no loaded grammars.

func (*Resrap) GenerateRandom

func (r *Resrap) GenerateRandom(name, starting_node string, tokens int) string

GenerateRandom generates content from the grammar identified by 'name'. starting_node: the starting heading in the grammar for generation. Returns a string containing the generated content. The generation is non-deterministic (random).

func (*Resrap) GenerateWithSeeded

func (r *Resrap) GenerateWithSeeded(name, starting_node string, seed uint64, tokens int) string

GenerateWithSeeded generates content from the grammar identified by 'name'. starting_node: the starting symbol in the grammar for generation. seed: a numeric seed to make generation deterministic. Returns a string containing the generated content.

func (*Resrap) ParseGrammar

func (r *Resrap) ParseGrammar(name, grammar string) error

ParseGrammar parses a grammar string and stores it under the given name. name: a unique identifier for this grammar (e.g., "C"), should be in ABNF format(Check osdc/resrap for more info on that). Returns error generated while parsing

func (*Resrap) ParseGrammarFile

func (r *Resrap) ParseGrammarFile(name, location string) error

ParseGrammarFile parses a grammar from a file and stores it under the given name. name: a unique identifier for this grammar (e.g., "C"), should be in ABNF format(Check osdc/resrap for more info on that). location: path to the grammar file. Returns error generated while parsing

type ResrapMT

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

ResrapMT is the multithreaded version of ResrapMT

func NewResrapMT

func NewResrapMT(poolsize, waitqueuesize int) *ResrapMT

NewResrapMT creates and returns a new Resrap MultiThreaded instance. The returned instance starts with no loaded grammars.

func (*ResrapMT) GenerateRandom

func (r *ResrapMT) GenerateRandom(id, name, starting_node string, tokens int)

GenerateRandom schedules a job to generate content from the grammar identified by 'name'. starting_node: the starting symbol in the grammar for generation. id: a user-defined process ID that will be associated with the generated content. tokens: number of tokens to generate. The generation is non-deterministic (random). The generated content will be sent asynchronously to the CodeChannel. Users must provide a unique process ID and retrieve the result via the get channel function.

func (*ResrapMT) GenerateWithSeeded

func (r *ResrapMT) GenerateWithSeeded(id, name, starting_node string, seed uint64, tokens int)

GenerateWithSeeded schedules a job to generate content from the grammar identified by 'name'. starting_node: the starting symbol in the grammar for generation. seed: a numeric seed to make generation deterministic. id: a user-defined process ID that will be associated with the generated content. tokens: number of tokens to generate. The generated content will be sent asynchronously to the CodeChannel. Users must provide a unique process ID and retrieve the result via the get channel function.

func (*ResrapMT) GetCodeChannel

func (r *ResrapMT) GetCodeChannel() chan CodeGenRes

GetCodeChannel is the main endpoint for the user to access the processed tokens

func (*ResrapMT) ParseGrammar

func (r *ResrapMT) ParseGrammar(name, grammar string) error

ParseGrammar parses a grammar string and stores it under the given name. name: a unique identifier for this grammar (e.g., "C"). grammar: the grammar definition as a string.

func (*ResrapMT) ParseGrammarFile

func (r *ResrapMT) ParseGrammarFile(name, location string) error

ParseGrammarFile parses a grammar from a file and stores it under the given name. name: a unique identifier for this grammar (e.g., "C"). location: path to the grammar file.

func (*ResrapMT) ShutDownResrap

func (r *ResrapMT) ShutDownResrap()

ShutDownResrap gracefully ends the server goroutines running

func (*ResrapMT) StartResrap

func (r *ResrapMT) StartResrap()

StartResrap boots up goroutines as your specified threadpool

type ScanError

type ScanError struct {
	Pos int
	Msg string
}

Jump to

Keyboard shortcuts

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