npeg

Source   Edit  

Note: This document is rather terse, for the complete NPeg manual please refer to the README.md or the git project page at https://github.com/zevv/npeg

NPeg is a pure Nim pattern matching library. It provides macros to compile patterns and grammars (PEGs) to Nim procedures which will parse a string and collect selected parts of the input. PEGs are not unlike regular expressions, but offer more power and flexibility, and have less ambiguities.

Here is a simple example showing the power of NPeg: The macro peg compiles a grammar definition into a parser object, which is used to match a string and place the key-value pairs into the Nim table words:

Example:

import npeg

import npeg, strutils, tables

var words: Table[string, int]

let parser = peg "pairs":
  pairs <- pair * *(',' * pair) * !1
  word <- +Alpha
  number <- +Digit
  pair <- >word * '=' * >number:
    words[$1] = parseInt($2)

doAssert parser.match("one=1,two=2,three=3,four=4").ok

Procs

proc captures(mr: MatchResult[char]): seq[string] {....raises: [], tags: [],
    forbids: [].}
Return all plain string captures from the match result Source   Edit  
proc captures[S](mr: MatchResult[S]): seq[S]
Return all plain string captures from the match result Source   Edit  
proc match[S, T](p: Parser; s: openArray[S]; userData: var T): MatchResult[S]
Match a subject string with the given generic parser. The returned MatchResult contains the result of the match and can be used to query any captures. Source   Edit  
proc match[S](p: Parser; s: openArray[S]): MatchResult[S]
Match a subject string with the given parser. The returned MatchResult contains the result of the match and can be used to query any captures. Source   Edit  
proc matchFile(p: Parser; fname: string): MatchResult[char]
Source   Edit  
proc matchFile[T](p: Parser; fname: string; userData: var T): MatchResult[char]
Source   Edit  

Macros

macro grammar(libNameNode: untyped; n: untyped)
This macro defines a collection of rules to be stored in NPeg's global grammar library. Source   Edit  
macro peg(name: untyped; n: untyped): untyped
Construct a parser from the given PEG grammar. name is the initial grammar rule where parsing starts. This macro returns a Parser type which can later be used for matching subjects with the match() proc Source   Edit  
macro peg(name: untyped; subjectType, userData, n: untyped): untyped

Construct a parser from the given PEG grammar. name is the initial grammar rule where parsing starts. This macro returns a Parser type which can later be used for matching subjects with the match() proc

The subjectType argument is a Nim type which should match the base type of the subject passed to match().

The userdata argument is a colon expression with an identifier and a type, this identifier is available in code block captions during parsing.

Source   Edit  
macro peg(name: untyped; userData: untyped; n: untyped): untyped

Construct a parser from the given PEG grammar. name is the initial grammar rule where parsing starts. This macro returns a Parser type which can later be used for matching subjects with the match() proc

The userdata argument is a colon expression with an identifier and a type, this identifier is available in code block captions during parsing.

Source   Edit  

Templates

template nimBug22740()
Provide stub templates as a workaround for https://github.com/nim-lang/Nim/issues/22740. Invoke this template in your code if you want to define a parser in a generic proc. Source   Edit  
template patt(n: untyped): untyped
Construct a parser from a single PEG rule. This is similar to the regular peg() macro, but useful for short regexp-like parsers that do not need a complete grammar. Source   Edit  
template patt(n: untyped; code: untyped): untyped
Construct a parser from a single PEG rule. This is similar to the regular peg() macro, but useful for short regexp-like parsers that do not need a complete grammar. This variant takes a code block which will be used as code block capture for the anonymous rule. Source   Edit