### Setting tsPEG Start Rule Source: https://github.com/eoindavey/tspeg/blob/master/README.md This snippet illustrates how to explicitly define the starting rule for the tsPEG parser using the `start` rule. By default, tsPEG starts parsing with the first rule defined in the grammar file. Defining a `start` rule allows you to specify a different entry point. ```tsPEG Grammar start := helloChoice hello := 'Hello ' helloWorld := hello 'World' helloChoice := hello 'Mars' | helloWorld ``` -------------------------------- ### Installing tsPEG CLI Source: https://github.com/eoindavey/tspeg/blob/master/README.md Command to install the tsPEG command-line tool globally using the npm package manager. ```Shell npm install -g tspeg ``` -------------------------------- ### Defining Basic tsPEG Rules Source: https://github.com/eoindavey/tspeg/blob/master/README.md This snippet demonstrates the fundamental syntax for defining rules in a tsPEG grammar. It shows how to match literal strings, sequence rules, and use the choice operator (`|`) to match one of several alternatives. The parser attempts to match the left side of the choice first. ```tsPEG Grammar hello := 'Hello ' helloWorld := hello 'World' helloChoice := hello 'Mars' | helloWorld ``` -------------------------------- ### Execute Compiled CLI (Shell) Source: https://github.com/eoindavey/tspeg/blob/master/demos/calculator_with_computed_properties/README.md Runs the compiled JavaScript command-line interface entry point using Node.js. ```shell node jsbuild/cli.js ``` -------------------------------- ### Generate Parser with tspeg (Shell) Source: https://github.com/eoindavey/tspeg/blob/master/demos/calculator_with_computed_properties/README.md Runs the tspeg command-line tool to generate the parser TypeScript file from the grammar definition file. ```shell tspeg grammar.gram parser.ts ``` -------------------------------- ### Compile Project with TypeScript (Shell) Source: https://github.com/eoindavey/tspeg/blob/master/demos/calculator_with_computed_properties/README.md Executes the TypeScript compiler (tsc) to compile the project based on the configuration file (tsconfig.json) in the current directory. ```shell tsc -p . ``` -------------------------------- ### Basic Grammar Without EOF Matching (tsPEG) Source: https://github.com/eoindavey/tspeg/blob/master/README.md This simple tsPEG grammar defines a rule `hello` that matches the literal string 'Hello World'. Without the EOF symbol, this grammar will match the prefix "Hello World" even if there is additional text following it. ```tsPEG hello := 'Hello World' ``` -------------------------------- ### tsPEG Grammar with Header for Imports Source: https://github.com/eoindavey/tspeg/blob/master/README.md Demonstrates the use of the header section (delimited by '---') in a tsPEG grammar file. Content within the header is inserted directly into the generated parser file, allowing imports of external types or functions. ```tsPEG Grammar --- import { myFunc, myType } from "./mypackage"; --- rule := hello='Hello World' .value = myType { return myFunc(this.hello); } ``` -------------------------------- ### Using Generated TypeScript AST Source: https://github.com/eoindavey/tspeg/blob/master/README.md This snippet demonstrates how to import and use the TypeScript interface generated by tsPEG (`sum`) to process the parsed AST. The function takes the AST object as input and accesses the captured data via its properties (`ast.left`, `ast.right`) to perform operations, such as parsing strings to integers and summing them. ```TypeScript import { sum } from "./parser"; export function add(ast: sum): number { return parseInt(ast.left) + parseInt(ast.right); } ``` -------------------------------- ### Basic tsPEG Grammar for Sum Source: https://github.com/eoindavey/tspeg/blob/master/README.md A simple tsPEG grammar rule definition for parsing a sum expression like '2+3', consisting of two numbers separated by a plus sign. ```tsPEG Grammar sum := left=num '\+' right=num num := '[0-9]+' ``` -------------------------------- ### Capturing Data in tsPEG AST Source: https://github.com/eoindavey/tspeg/blob/master/README.md This snippet shows how to assign matched parts of the input to named fields within a rule definition using the `=` operator. These assignments instruct tsPEG to capture the matched content and include it as properties in the generated Abstract Syntax Tree (AST). It also demonstrates escaping special regex characters like `+` using `\+`. ```tsPEG Grammar sum := left=num '\+' right=num num := '[0-9]+' ``` -------------------------------- ### Defining a Simple Sum Grammar (tsPEG) Source: https://github.com/eoindavey/tspeg/blob/master/README.md This tsPEG grammar defines a rule `sum` that matches two numbers separated by a plus sign. The `left` and `right` parts capture the matched numbers. ```tsPEG sum := left=num '\+' right=num num := '[0-9]+' ``` -------------------------------- ### Generated AST Interface with Position Info (TypeScript) Source: https://github.com/eoindavey/tspeg/blob/master/README.md This TypeScript interface shows the structure of the Abstract Syntax Tree (AST) node generated by tsPEG for the `sum` rule when the position of the '+' operator is captured using `@`. It includes fields for the left and right numbers (as strings) and the operator's position (as `PosInfo`). ```TypeScript interface sum { left: string pluspos: PosInfo right: string } ``` -------------------------------- ### Using Sub-rules with Optional Operator (?) in tsPEG Grammar Source: https://github.com/eoindavey/tspeg/blob/master/README.md Demonstrates defining an inline sub-rule using `{}` brackets and applying the optional `?` operator to the entire sub-rule. This allows a group of expressions to be treated as a single optional unit within a larger rule. ```tsPEG Grammar rule := 'start' {some optional part}? 'finish' ``` -------------------------------- ### TypeScript Interface for PosInfo Source: https://github.com/eoindavey/tspeg/blob/master/README.md TypeScript interface definition for the 'PosInfo' object. This object is returned by the special '@' expression in tsPEG grammars and contains details about the parser's position in the input string (overall index, line number, and offset within the line). ```TypeScript export interface PosInfo { // overallPos is the index of the input string readonly overallPos: number; // line is the line of the input readonly line: number; // offset is the number of characters from the // start of the line readonly offset: number; } ``` -------------------------------- ### tsPEG Grammar for Choice and Kind Checking Source: https://github.com/eoindavey/tspeg/blob/master/README.md A tsPEG grammar defining rules with choices ('Choice' rule) and simple literal matches ('Word', 'Int'). This structure is used to illustrate how AST nodes generated from different choices can be distinguished using their 'kind' property. ```tsPEG Grammar Choice := Word | Int Word := word='[a-z]+' Int := val='[0-9]+' ``` -------------------------------- ### Using Generated tsPEG Parser (TypeScript) Source: https://github.com/eoindavey/tspeg/blob/master/README.md Defines the structure of the ParseResult object returned by the generated parser function and shows the function signature. It indicates how to access the Abstract Syntax Tree (AST) or syntax errors after parsing. ```TypeScript class ParseResult { ast: START | null; errs: SyntaxErr[]; } export function parse(input: string): ParseResult { ... } ``` -------------------------------- ### Generated TypeScript AST Interface Source: https://github.com/eoindavey/tspeg/blob/master/README.md This snippet shows the TypeScript interface automatically generated by tsPEG for the `sum` rule defined in the grammar. The fields (`left`, `right`) assigned in the grammar rule become properties of this interface, allowing type-safe access to the captured data in your TypeScript code. ```TypeScript interface sum { left: string; right: string; } ``` -------------------------------- ### Defining Syntax Error Structure in tsPEG (TypeScript) Source: https://github.com/eoindavey/tspeg/blob/master/README.md Provides the TypeScript definitions for the `SyntaxErr` class and related types (`MatchAttempt`, `RegexMatch`, `EOFMatch`, `PosInfo`). These structures represent the details of a parsing error, including its position and the expected matches at the error location. ```TypeScript interface RegexMatch { kind: "RegexMatch"; negated: boolean; literal: string; } type EOFMatch = { kind: "EOF"; negated: boolean }; type MatchAttempt = RegexMatch | EOFMatch; interface PosInfo { overallPos: number; line: number; offset: number; } class SyntaxErr { public pos: PosInfo; public expmatches: MatchAttempt[]; public toString(): string { // ... } } ``` -------------------------------- ### TypeScript Function Using AST Kind Checking Source: https://github.com/eoindavey/tspeg/blob/master/README.md A TypeScript function that processes an AST node generated by the 'Choice' grammar rule. It demonstrates how to use the 'kind' property and the 'ASTKinds' enum to determine which specific rule ('Word' or 'Int') was matched and access the corresponding properties. ```TypeScript import { Choice, Parser, ASTKinds } from "./parser"; function makeChoice(c: Choice) { if(c.kind === ASTKinds.Word) { console.log('Matched word ', c.word) } else { console.log('Matched int ', c.val) } } ``` -------------------------------- ### Using the Optional Operator (?) in tsPEG Grammar Source: https://github.com/eoindavey/tspeg/blob/master/README.md Demonstrates the use of the `?` operator to make the preceding expression ('really ') optional within a tsPEG grammar rule. This rule matches either "I really love tsPEG" or "I love tsPEG". ```tsPEG Grammar rule := 'I ' 'really '? 'love tsPEG' ``` -------------------------------- ### tsPEG Grammar with Computed Property on Sum Rule Source: https://github.com/eoindavey/tspeg/blob/master/README.md Further extends the grammar by adding a computed property '.sum' to the 'sum' rule. This property calculates the sum of the '.value' computed properties from the 'left' and 'right' child nodes. ```tsPEG Grammar sum := left=num '\+' right=num .sum = number { return this.left.value + this.right.value } num := literal='[0-9]+' .value = number { return parseInt(this.literal); } ``` -------------------------------- ### tsPEG Grammar with Computed Property on Num Rule Source: https://github.com/eoindavey/tspeg/blob/master/README.md Extends the basic sum grammar by adding a computed property '.value' to the 'num' rule. This property parses the matched literal string ('[0-9]+') into a number using 'parseInt' at parse time. ```tsPEG Grammar sum := left=num '\+' right=num num := literal='[0-9]+' .value = number { return parseInt(this.literal); } ``` -------------------------------- ### Capturing Operator Position in Sum Grammar (tsPEG) Source: https://github.com/eoindavey/tspeg/blob/master/README.md This modified tsPEG grammar adds the `@` match operator to capture the position of the '+' symbol before it is consumed. The captured position is stored in the `pluspos` field. ```tsPEG sum := left=num pluspos=@ '\+' right=num num := '[0-9]+' ``` -------------------------------- ### Using the Negative Lookahead Operator (!) in tsPEG Grammar Source: https://github.com/eoindavey/tspeg/blob/master/README.md Shows the `!` operator (negative lookahead), which asserts that the following expression ('Macbeth') *must not* match at the current position. The rule matches "The banned word is X" where X is any sequence of letters except "Macbeth". ```tsPEG Grammar rule := 'The banned word is ' !'Macbeth' '[a-zA-Z]+' ``` -------------------------------- ### Grammar Requiring EOF Matching (tsPEG) Source: https://github.com/eoindavey/tspeg/blob/master/README.md This tsPEG grammar modifies the `hello` rule by adding the `$` symbol. This requires that the parser must reach the end of the input string for the match to succeed, ensuring the entire input is consumed. ```tsPEG hello := 'Hello World' $ ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.