Skip to content

Instantly share code, notes, and snippets.

@DmitrySoshnikov
DmitrySoshnikov / regexp-tree-processing.js
Last active April 4, 2017 06:31
regexp-tree-processing
const regexpTree = require('regexp-tree');
// Get AST.
const ast = regexpTree.parse('/[a-z]{1,}/');
// Handle nodes.
regexpTree.traverse(ast, {
// Handle "Quantifier" node type,
// transforming `{1,}` quantifier to `+`.
@DmitrySoshnikov
DmitrySoshnikov / ES7 Notes.txt
Last active February 9, 2018 19:42
ES7 Notes
// by Dmitry Soshnikov
ES7:
== Lexical environment ==
- Environment Record
- parent (can be null)
== Environment Record ==
const regexpTree = require('regexp-tree');
console.log(regexpTree.parse(/a|b/i)); // RegExp AST
@DmitrySoshnikov
DmitrySoshnikov / regexp-tree-example1.json
Last active March 30, 2017 05:39
regexp-tree-example1.json
{
"type": "RegExp",
"body": {
"type": "Repetition",
"expression": {
"type": "CharacterClass",
"expressions": [
{
"type": "ClassRange",
"from": {
@DmitrySoshnikov
DmitrySoshnikov / Boolean.parse.md
Last active March 21, 2017 20:10
ES proposal: Boolean.parse

Boolean.parse(string)

Specification

Parses the string argument as a boolean. The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string "true".

When the Boolean.parse function is called with a string argument the following steps are taken:

  1. If Type(string) is not "string"
  • Return false
// RegExp:
/x*?/
// 1. Char is main, repetition is secondary (modifier)
{
type: 'Char',
value: 'x',
modifier: {
@DmitrySoshnikov
DmitrySoshnikov / 1.grammar.ll1
Created November 29, 2016 00:33
LL(1) boolean
%%
boolean
: term bool'
;
bool'
: 'OR' term bool'
| /* epsilon */
;
@DmitrySoshnikov
DmitrySoshnikov / grammar-with-ops-assoc-table.txt
Last active October 24, 2016 01:26
Comparison of parsing tables for grammars with and without operators precedence and assoc
.dmitrys:syntax$ syntax-cli --grammar ~/calculator.grammar.js --mode LALR1 --table
Parsing mode: LALR(1).
Grammar:
0. $accept -> E
----------------
1. E -> E + E
2. | E * E
@DmitrySoshnikov
DmitrySoshnikov / calc-no-assoc.g.js
Created October 24, 2016 01:19
calc-no-assoc.g.js
/**
* Explicit handling of the operator associativity. See also implicit handling
* https://gist.github.com/DmitrySoshnikov/ab6ed4d41505c579cee9af759e77ead9
*
* Calculator grammar for a parser in Python.
*
* syntax-cli -g calc-no-assoc.g.js -m LALR1 -o calcparser.py
*
* >>> import calcparser
* >>> calcparser.parse('2 + 2 * 2')
@DmitrySoshnikov
DmitrySoshnikov / lex-start-conditions.md
Last active April 26, 2023 11:46
Lexer start conditions

Start conditions of lex rules, and tokenizer states

Start conditions are declared in the definitions (first) section of the input using unindented lines beginning with either %s or %x followed by a list of names. The former declares inclusive start conditions, the latter exclusive start conditions. A start condition is activated using the BEGIN action. Until the next BEGIN action is executed, rules with the given start condition will be active and rules with other start conditions will be inactive. If the start condition is inclusive, then rules with no start conditions at all will also be active. If it is exclusive, then only rules qualified with the start condition will be active. A set of rules contingent on the same exclusive start condition describe a scanner which is independent of any of the other rules in the flex input. Because of this, exclusive start conditions make it easy to specify "mini-scanners" which scan portions of the input that are syntactically different from the res