You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: API.md
+97-18
Original file line number
Diff line number
Diff line change
@@ -16,6 +16,41 @@ A parser is said to *consume* the text that it parses, leaving only the unconsum
16
16
17
17
These are either parsers or functions that return new parsers. These are the building blocks of parsers. They are all contained in the `Parsimmon` object.
18
18
19
+
## Parsimmon.createLanguage(parsers)
20
+
21
+
`createLanguage` is the best starting point for building a language parser in Parsimmon. It organizes all of your parsers, collects them into a single namespace, and removes the need to worry about using `Parsimmon.lazy`.
22
+
23
+
Each function passed to `createLanguage` receives as its only parameter the entire language of parsers as an object. This is used for referring to other rules from within your current rule.
**NOTE:** You probably will never need to use this function. Most parsing can be accomplished using `Parsimmon.regexp` and combination with `Parsimmon.seq` and `Parsimmon.alt`.
@@ -182,6 +217,8 @@ This is the same as `Parsimmon.sepBy`, but matches the `content` parser **at lea
182
217
183
218
## Parsimmon.lazy(fn)
184
219
220
+
**NOTE:** This is not needed if you're using `createLanguage`.
221
+
185
222
Accepts a function that returns a parser, which is evaluated the first time the parser is used. This is useful for referencing parsers that haven't yet been defined, and for implementing recursive parsers. Example:
186
223
187
224
```javascript
@@ -523,29 +560,71 @@ Expects `parser` at most `n` times. Yields an array of the results.
523
560
524
561
Expects `parser` at least `n` times. Yields an array of the results.
525
562
563
+
## parser.node(name)
564
+
565
+
Yields an object with `name`, `value`, `start`, and `end` keys, where `value` is the original value yielded by the parser, `name` is the argument passed in, and `start` and `end` are are objects with a 0-based `offset` and 1-based `line` and `column` properties that represent the position in the input that contained the parsed text.
566
+
567
+
Example:
568
+
569
+
```javascript
570
+
var Identifier =
571
+
Parsimmon.regexp(/[a-z]+/).node('Identifier');
572
+
573
+
Identifier.tryParse('hey');
574
+
// => { name: 'Identifier',
575
+
// value: 'hey',
576
+
// start: { offset: 0, line: 1, column: 1 },
577
+
// end: { offset: 3, line: 1, column: 4 } }
578
+
```
579
+
526
580
## parser.mark()
527
581
528
-
Yields an object with `start`, `value`, and `end` keys,
529
-
where `value` is the original value yielded by the parser, and `start` and
530
-
`end` are are objects with a 0-based `offset` and 1-based `line` and
531
-
`column` properties that represent the position in the input that
532
-
contained the parsed text. Works like this function:
582
+
Yields an object with `start`, `value`, and `end` keys, where `value` is the original value yielded by the parser, and `start` and `end` are are objects with a 0-based `offset` and 1-based `line` and `column` properties that represent the position in the input that contained the parsed text. Works like this function:
533
583
534
584
```javascript
535
-
functionmark(parser) {
536
-
returnParsimmon.seqMap(
537
-
Parsimmon.index,
538
-
parser,
539
-
Parsimmon.index,
540
-
function(start, value, end) {
541
-
return {
542
-
start: start,
543
-
value: value,
544
-
end: end
545
-
};
546
-
}
547
-
);
585
+
var Identifier =
586
+
Parsimmon.regexp(/[a-z]+/).mark();
587
+
588
+
Identifier.tryParse('hey');
589
+
// => { start: { offset: 0, line: 1, column: 1 },
590
+
// value: 'hey',
591
+
// end: { offset: 3, line: 1, column: 4 } }
592
+
```
593
+
594
+
## parser.thru(wrapper)
595
+
596
+
Simply returns `wrapper(this)` from the parser. Useful for custom functions used to wrap your parsers, while keeping with Parsimmon chaining style.
0 commit comments