Added combinator
This commit is contained in:
parent
5b6f6bdf97
commit
55beadd737
50 changed files with 2540 additions and 1 deletions
|
@ -2,7 +2,9 @@
|
|||
* Changed Dart SDK requirements for all packages to ">=2.12.0 <3.0.0" to support NNBD.
|
||||
* Updated pretty_logging to 3.0.0
|
||||
* Updated angel_http_exception to 3.0.0
|
||||
* Moved to https://github.com/dukefirehawk/cli
|
||||
* Moved angel_cli to https://github.com/dukefirehawk/cli
|
||||
* Added code_buffer 2.0.0
|
||||
* Added combinator 2.0.0
|
||||
* Updated angel_route to 5.0.0
|
||||
|
||||
# 3.0.0 (Non NNBD)
|
||||
|
|
66
packages/combinator/.gitignore
vendored
Normal file
66
packages/combinator/.gitignore
vendored
Normal file
|
@ -0,0 +1,66 @@
|
|||
# Created by .ignore support plugin (hsz.mobi)
|
||||
### Dart template
|
||||
# See https://www.dartlang.org/tools/private-files.html
|
||||
|
||||
# Files and directories created by pub
|
||||
.packages
|
||||
.pub/
|
||||
build/
|
||||
# If you're building an application, you may want to check-in your pubspec.lock
|
||||
pubspec.lock
|
||||
|
||||
# Directory created by dartdoc
|
||||
# If you don't generate documentation locally you can remove this line.
|
||||
doc/api/
|
||||
### JetBrains template
|
||||
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
|
||||
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
||||
|
||||
# User-specific stuff:
|
||||
.idea/**/workspace.xml
|
||||
.idea/**/tasks.xml
|
||||
.idea/dictionaries
|
||||
|
||||
# Sensitive or high-churn files:
|
||||
.idea/**/dataSources/
|
||||
.idea/**/dataSources.ids
|
||||
.idea/**/dataSources.xml
|
||||
.idea/**/dataSources.local.xml
|
||||
.idea/**/sqlDataSources.xml
|
||||
.idea/**/dynamic.xml
|
||||
.idea/**/uiDesigner.xml
|
||||
|
||||
# Gradle:
|
||||
.idea/**/gradle.xml
|
||||
.idea/**/libraries
|
||||
|
||||
# CMake
|
||||
cmake-build-debug/
|
||||
|
||||
# Mongo Explorer plugin:
|
||||
.idea/**/mongoSettings.xml
|
||||
|
||||
## File-based project format:
|
||||
*.iws
|
||||
|
||||
## Plugin-specific files:
|
||||
|
||||
# IntelliJ
|
||||
out/
|
||||
|
||||
# mpeltonen/sbt-idea plugin
|
||||
.idea_modules/
|
||||
|
||||
# JIRA plugin
|
||||
atlassian-ide-plugin.xml
|
||||
|
||||
# Cursive Clojure plugin
|
||||
.idea/replstate.xml
|
||||
|
||||
# Crashlytics plugin (for Android Studio and IntelliJ)
|
||||
com_crashlytics_export_strings.xml
|
||||
crashlytics.properties
|
||||
crashlytics-build.properties
|
||||
fabric.properties
|
||||
|
||||
.dart_tool
|
4
packages/combinator/.travis.yml
Normal file
4
packages/combinator/.travis.yml
Normal file
|
@ -0,0 +1,4 @@
|
|||
language: dart
|
||||
dart:
|
||||
- stable
|
||||
- dev
|
11
packages/combinator/CHANGELOG.md
Normal file
11
packages/combinator/CHANGELOG.md
Normal file
|
@ -0,0 +1,11 @@
|
|||
# 1.1.0
|
||||
* Add `tupleX` parsers. Hooray for strong typing!
|
||||
|
||||
# 1.0.0+3
|
||||
* `then` now *always* returns `dynamic`.
|
||||
|
||||
# 1.0.0+2
|
||||
* `star` now includes with a call to `opt`.
|
||||
* Added comments.
|
||||
* Enforce generics on `separatedBy`.
|
||||
* Enforce Dart 2 semantics.
|
21
packages/combinator/LICENSE
Normal file
21
packages/combinator/LICENSE
Normal file
|
@ -0,0 +1,21 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) 2017 Tobe O
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
120
packages/combinator/README.md
Normal file
120
packages/combinator/README.md
Normal file
|
@ -0,0 +1,120 @@
|
|||
# combinator
|
||||
[![version](https://img.shields.io/pub/v/combinator.svg)](https://pub.dartlang.org/packages/combinator)
|
||||
[![build status](https://travis-ci.org/thosakwe/combinator.svg)](https://travis-ci.org/thosakwe/combinator)
|
||||
|
||||
Packrat parser combinators that support static typing, generics, file spans, memoization, and more.
|
||||
|
||||
**RECOMMENDED:**
|
||||
Check `example/` for examples.
|
||||
The examples contain examples of using:
|
||||
* Generic typing
|
||||
* Reading `FileSpan` from `ParseResult`
|
||||
* More...
|
||||
|
||||
## Basic Usage
|
||||
```dart
|
||||
void main() {
|
||||
// Parse a Pattern (usually String or RegExp).
|
||||
var foo = match('foo');
|
||||
var number = match(new RegExp(r'[0-9]+'), errorMessage: 'Expected a number.');
|
||||
|
||||
// Set a value.
|
||||
var numWithValue = number.map((r) => int.parse(r.span.text));
|
||||
|
||||
// Expect a pattern, or nothing.
|
||||
var optional = numWithValue.opt();
|
||||
|
||||
// Expect a pattern zero or more times.
|
||||
var star = optional.star();
|
||||
|
||||
// Expect one or more times.
|
||||
var plus = optional.plus();
|
||||
|
||||
// Expect an arbitrary number of times.
|
||||
var threeTimes = optional.times(3);
|
||||
|
||||
// Expect a sequence of patterns.
|
||||
var doraTheExplorer = chain([
|
||||
match('Dora').space(),
|
||||
match('the').space(),
|
||||
match('Explorer').space(),
|
||||
]);
|
||||
|
||||
// Choose exactly one of a set of patterns, whichever
|
||||
// appears first.
|
||||
var alt = any([
|
||||
match('1'),
|
||||
match('11'),
|
||||
match('111'),
|
||||
]);
|
||||
|
||||
// Choose the *longest* match for any of the given alternatives.
|
||||
var alt2 = longest([
|
||||
match('1'),
|
||||
match('11'),
|
||||
match('111'),
|
||||
]);
|
||||
|
||||
// Friendly operators
|
||||
var fooOrNumber = foo | number;
|
||||
var fooAndNumber = foo & number;
|
||||
var notFoo = ~foo;
|
||||
}
|
||||
```
|
||||
|
||||
## Error Messages
|
||||
Parsers without descriptive error messages can lead to frustrating dead-ends
|
||||
for end-users. Fortunately, `combinator` is built with error handling in mind.
|
||||
|
||||
```dart
|
||||
void main(Parser parser) {
|
||||
// Append an arbitrary error message to a parser if it is not matched.
|
||||
var withError = parser.error(errorMessage: 'Hey!!! Wrong!!!');
|
||||
|
||||
// You can also set the severity of an error.
|
||||
var asHint = parser.error(severity: SyntaxErrorSeverity.hint);
|
||||
|
||||
// Constructs like `any`, `chain`, and `longest` support this as well.
|
||||
var foo = longest([
|
||||
parser.error(errorMessage: 'foo'),
|
||||
parser.error(errorMessage: 'bar')
|
||||
], errorMessage: 'Expected a "foo" or a "bar"');
|
||||
|
||||
// If multiple errors are present at one location,
|
||||
// it can create a lot of noise.
|
||||
//
|
||||
// Use `foldErrors` to only take one error at a given location.
|
||||
var lessNoise = parser.foldErrors();
|
||||
}
|
||||
```
|
||||
|
||||
## Whitespaces
|
||||
Handling optional whitespace is dead-easy:
|
||||
|
||||
```dart
|
||||
void main(Parser parser) {
|
||||
var optionalSpace = parser.space();
|
||||
}
|
||||
```
|
||||
|
||||
## For Programming Languages
|
||||
`combinator` was conceived to make writing parsers for complex grammars easier,
|
||||
namely programming languages. Thus, there are functions built-in to make common constructs
|
||||
easier:
|
||||
|
||||
```dart
|
||||
void main(Parser parser) {
|
||||
var array = parser
|
||||
.separatedByComma()
|
||||
.surroundedBySquareBrackets(defaultValue: []);
|
||||
|
||||
var braces = parser.surroundedByCurlyBraces();
|
||||
|
||||
var sep = parser.separatedBy(match('!').space());
|
||||
}
|
||||
```
|
||||
|
||||
## Differences between this and Petitparser
|
||||
* `combinator` makes extensive use of Dart's dynamic typing
|
||||
* `combinator` supports detailed error messages (with configurable severity)
|
||||
* `combinator` keeps track of locations (ex. `line 1: 3`)
|
4
packages/combinator/analysis_options.yaml
Normal file
4
packages/combinator/analysis_options.yaml
Normal file
|
@ -0,0 +1,4 @@
|
|||
analyzer:
|
||||
strong-mode:
|
||||
implicit-casts: false
|
||||
#implicit-dynamic: false
|
14
packages/combinator/combinator.iml
Normal file
14
packages/combinator/combinator.iml
Normal file
|
@ -0,0 +1,14 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="WEB_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<excludeFolder url="file://$MODULE_DIR$/.dart_tool" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/.pub" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/build" />
|
||||
</content>
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="library" name="Dart SDK" level="project" />
|
||||
<orderEntry type="library" name="Dart Packages" level="project" />
|
||||
</component>
|
||||
</module>
|
55
packages/combinator/example/basic_auth.dart
Normal file
55
packages/combinator/example/basic_auth.dart
Normal file
|
@ -0,0 +1,55 @@
|
|||
// Run this with "Basic QWxhZGRpbjpPcGVuU2VzYW1l"
|
||||
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'package:combinator/combinator.dart';
|
||||
import 'package:string_scanner/string_scanner.dart';
|
||||
|
||||
/// Parse a part of a decoded Basic auth string.
|
||||
///
|
||||
/// Namely, the `username` or `password` in `{username}:{password}`.
|
||||
final Parser<String> string =
|
||||
match<String>(new RegExp(r'[^:$]+'), errorMessage: 'Expected a string.')
|
||||
.value((r) => r.span.text);
|
||||
|
||||
/// Transforms `{username}:{password}` to `{"username": username, "password": password}`.
|
||||
final Parser<Map<String, String>> credentials = chain<String>([
|
||||
string.opt(),
|
||||
match<String>(':'),
|
||||
string.opt(),
|
||||
]).map<Map<String, String>>(
|
||||
(r) => {'username': r.value[0], 'password': r.value[2]});
|
||||
|
||||
/// We can actually embed a parser within another parser.
|
||||
///
|
||||
/// This is used here to BASE64URL-decode a string, and then
|
||||
/// parse the decoded string.
|
||||
final Parser credentialString = match<Map<String, String>>(
|
||||
new RegExp(r'([^\n$]+)'),
|
||||
errorMessage: 'Expected a credential string.')
|
||||
.value((r) {
|
||||
var decoded = utf8.decode(base64Url.decode(r.span.text));
|
||||
var scanner = new SpanScanner(decoded);
|
||||
return credentials.parse(scanner).value;
|
||||
});
|
||||
|
||||
final Parser basic = match<Null>('Basic').space();
|
||||
|
||||
final Parser basicAuth = basic.then(credentialString).index(1);
|
||||
|
||||
void main() {
|
||||
while (true) {
|
||||
stdout.write('Enter a basic auth value: ');
|
||||
var line = stdin.readLineSync();
|
||||
var scanner = new SpanScanner(line, sourceUrl: 'stdin');
|
||||
var result = basicAuth.parse(scanner);
|
||||
|
||||
if (!result.successful) {
|
||||
for (var error in result.errors) {
|
||||
print(error.toolString);
|
||||
print(error.span.highlight(color: true));
|
||||
}
|
||||
} else
|
||||
print(result.value);
|
||||
}
|
||||
}
|
70
packages/combinator/example/calculator.dart
Normal file
70
packages/combinator/example/calculator.dart
Normal file
|
@ -0,0 +1,70 @@
|
|||
import 'dart:math';
|
||||
import 'dart:io';
|
||||
import 'package:combinator/combinator.dart';
|
||||
import 'package:string_scanner/string_scanner.dart';
|
||||
|
||||
/// Note: This grammar does not handle precedence, for the sake of simplicity.
|
||||
Parser<num> calculatorGrammar() {
|
||||
var expr = reference<num>();
|
||||
|
||||
var number = match<num>(new RegExp(r'-?[0-9]+(\.[0-9]+)?'))
|
||||
.value((r) => num.parse(r.span.text));
|
||||
|
||||
var hex = match<int>(new RegExp(r'0x([A-Fa-f0-9]+)'))
|
||||
.map((r) => int.parse(r.scanner.lastMatch[1], radix: 16));
|
||||
|
||||
var binary = match<int>(new RegExp(r'([0-1]+)b'))
|
||||
.map((r) => int.parse(r.scanner.lastMatch[1], radix: 2));
|
||||
|
||||
var alternatives = <Parser<num>>[];
|
||||
|
||||
void registerBinary(String op, num Function(num, num) f) {
|
||||
alternatives.add(
|
||||
chain<num>([
|
||||
expr.space(),
|
||||
match<Null>(op).space(),
|
||||
expr.space(),
|
||||
]).map((r) => f(r.value[0], r.value[2])),
|
||||
);
|
||||
}
|
||||
|
||||
registerBinary('**', (a, b) => pow(a, b));
|
||||
registerBinary('*', (a, b) => a * b);
|
||||
registerBinary('/', (a, b) => a / b);
|
||||
registerBinary('%', (a, b) => a % b);
|
||||
registerBinary('+', (a, b) => a + b);
|
||||
registerBinary('-', (a, b) => a - b);
|
||||
registerBinary('^', (a, b) => a.toInt() ^ b.toInt());
|
||||
registerBinary('&', (a, b) => a.toInt() & b.toInt());
|
||||
registerBinary('|', (a, b) => a.toInt() | b.toInt());
|
||||
|
||||
alternatives.addAll([
|
||||
number,
|
||||
hex,
|
||||
binary,
|
||||
expr.parenthesized(),
|
||||
]);
|
||||
|
||||
expr.parser = longest(alternatives);
|
||||
|
||||
return expr;
|
||||
}
|
||||
|
||||
void main() {
|
||||
var calculator = calculatorGrammar();
|
||||
|
||||
while (true) {
|
||||
stdout.write('Enter an expression: ');
|
||||
var line = stdin.readLineSync();
|
||||
var scanner = new SpanScanner(line, sourceUrl: 'stdin');
|
||||
var result = calculator.parse(scanner);
|
||||
|
||||
if (!result.successful) {
|
||||
for (var error in result.errors) {
|
||||
stderr.writeln(error.toolString);
|
||||
stderr.writeln(error.span.highlight(color: true));
|
||||
}
|
||||
} else
|
||||
print(result.value);
|
||||
}
|
||||
}
|
28
packages/combinator/example/delimiter.dart
Normal file
28
packages/combinator/example/delimiter.dart
Normal file
|
@ -0,0 +1,28 @@
|
|||
import 'dart:io';
|
||||
import 'package:combinator/combinator.dart';
|
||||
import 'package:string_scanner/string_scanner.dart';
|
||||
|
||||
final Parser<String> id =
|
||||
match<String>(RegExp(r'[A-Za-z]+')).value((r) => r.span.text);
|
||||
|
||||
// We can use `separatedBy` to easily construct parser
|
||||
// that can be matched multiple times, separated by another
|
||||
// pattern.
|
||||
//
|
||||
// This is useful for parsing arrays or map literals.
|
||||
main() {
|
||||
while (true) {
|
||||
stdout.write('Enter a string (ex "a,b,c"): ');
|
||||
var line = stdin.readLineSync();
|
||||
var scanner = new SpanScanner(line, sourceUrl: 'stdin');
|
||||
var result = id.separatedBy(match(',').space()).parse(scanner);
|
||||
|
||||
if (!result.successful) {
|
||||
for (var error in result.errors) {
|
||||
print(error.toolString);
|
||||
print(error.span.highlight(color: true));
|
||||
}
|
||||
} else
|
||||
print(result.value);
|
||||
}
|
||||
}
|
70
packages/combinator/example/json.dart
Normal file
70
packages/combinator/example/json.dart
Normal file
|
@ -0,0 +1,70 @@
|
|||
import 'dart:io';
|
||||
import 'package:combinator/combinator.dart';
|
||||
import 'package:string_scanner/string_scanner.dart';
|
||||
|
||||
Parser jsonGrammar() {
|
||||
var expr = reference();
|
||||
|
||||
// Parse a number
|
||||
var number = match<num>(new RegExp(r'-?[0-9]+(\.[0-9]+)?'),
|
||||
errorMessage: 'Expected a number.')
|
||||
.value(
|
||||
(r) => num.parse(r.span.text),
|
||||
);
|
||||
|
||||
// Parse a string (no escapes supported, because lazy).
|
||||
var string =
|
||||
match(new RegExp(r'"[^"]*"'), errorMessage: 'Expected a string.').value(
|
||||
(r) => r.span.text.substring(1, r.span.text.length - 1),
|
||||
);
|
||||
|
||||
// Parse an array
|
||||
var array = expr
|
||||
.space()
|
||||
.separatedByComma()
|
||||
.surroundedBySquareBrackets(defaultValue: []);
|
||||
|
||||
// KV pair
|
||||
var keyValuePair = chain([
|
||||
string.space(),
|
||||
match(':').space(),
|
||||
expr.error(errorMessage: 'Missing expression.'),
|
||||
]).castDynamic().cast<Map>().value((r) => {r.value[0]: r.value[2]});
|
||||
|
||||
// Parse an object.
|
||||
var object = keyValuePair
|
||||
.separatedByComma()
|
||||
.castDynamic()
|
||||
.surroundedByCurlyBraces(defaultValue: {});
|
||||
|
||||
expr.parser = longest(
|
||||
[
|
||||
array,
|
||||
number,
|
||||
string,
|
||||
object.error(),
|
||||
],
|
||||
errorMessage: 'Expected an expression.',
|
||||
).space();
|
||||
|
||||
return expr.foldErrors();
|
||||
}
|
||||
|
||||
main() {
|
||||
var JSON = jsonGrammar();
|
||||
|
||||
while (true) {
|
||||
stdout.write('Enter some JSON: ');
|
||||
var line = stdin.readLineSync();
|
||||
var scanner = new SpanScanner(line, sourceUrl: 'stdin');
|
||||
var result = JSON.parse(scanner);
|
||||
|
||||
if (!result.successful) {
|
||||
for (var error in result.errors) {
|
||||
print(error.toolString);
|
||||
print(error.span.highlight(color: true));
|
||||
}
|
||||
} else
|
||||
print(result.value);
|
||||
}
|
||||
}
|
37
packages/combinator/example/main.dart
Normal file
37
packages/combinator/example/main.dart
Normal file
|
@ -0,0 +1,37 @@
|
|||
import 'dart:io';
|
||||
import 'package:combinator/combinator.dart';
|
||||
import 'package:string_scanner/string_scanner.dart';
|
||||
|
||||
final Parser minus = match('-');
|
||||
|
||||
final Parser<int> digit =
|
||||
match(new RegExp(r'[0-9]'), errorMessage: 'Expected a number');
|
||||
|
||||
final Parser digits = digit.plus();
|
||||
|
||||
final Parser dot = match('.');
|
||||
|
||||
final Parser decimal = ( // digits, (dot, digits)?
|
||||
digits & (dot & digits).opt() //
|
||||
);
|
||||
|
||||
final Parser number = //
|
||||
(minus.opt() & decimal) // minus?, decimal
|
||||
.map<num>((r) => num.parse(r.span.text));
|
||||
|
||||
main() {
|
||||
while (true) {
|
||||
stdout.write('Enter a number: ');
|
||||
var line = stdin.readLineSync();
|
||||
var scanner = new SpanScanner(line, sourceUrl: 'stdin');
|
||||
var result = number.parse(scanner);
|
||||
|
||||
if (!result.successful) {
|
||||
for (var error in result.errors) {
|
||||
stderr.writeln(error.toolString);
|
||||
stderr.writeln(error.span.highlight(color: true));
|
||||
}
|
||||
} else
|
||||
print(result.value);
|
||||
}
|
||||
}
|
44
packages/combinator/example/query_string.dart
Normal file
44
packages/combinator/example/query_string.dart
Normal file
|
@ -0,0 +1,44 @@
|
|||
// For some reason, this cannot be run in checked mode???
|
||||
|
||||
import 'dart:io';
|
||||
import 'package:combinator/combinator.dart';
|
||||
import 'package:string_scanner/string_scanner.dart';
|
||||
|
||||
final Parser<String> key =
|
||||
match<String>(RegExp(r'[^=&\n]+'), errorMessage: 'Missing k/v')
|
||||
.value((r) => r.span.text);
|
||||
|
||||
final Parser value = key.map((r) => Uri.decodeQueryComponent(r.value));
|
||||
|
||||
final Parser pair = chain([
|
||||
key,
|
||||
match('='),
|
||||
value,
|
||||
]).map((r) {
|
||||
return {
|
||||
r.value[0]: r.value[2],
|
||||
};
|
||||
});
|
||||
|
||||
final Parser pairs = pair
|
||||
.separatedBy(match(r'&'))
|
||||
.map((r) => r.value.reduce((a, b) => a..addAll(b)));
|
||||
|
||||
final Parser queryString = pairs.opt();
|
||||
|
||||
main() {
|
||||
while (true) {
|
||||
stdout.write('Enter a query string: ');
|
||||
var line = stdin.readLineSync();
|
||||
var scanner = new SpanScanner(line, sourceUrl: 'stdin');
|
||||
var result = pairs.parse(scanner);
|
||||
|
||||
if (!result.successful) {
|
||||
for (var error in result.errors) {
|
||||
print(error.toolString);
|
||||
print(error.span.highlight(color: true));
|
||||
}
|
||||
} else
|
||||
print(result.value);
|
||||
}
|
||||
}
|
84
packages/combinator/example/sexp.dart
Normal file
84
packages/combinator/example/sexp.dart
Normal file
|
@ -0,0 +1,84 @@
|
|||
import 'dart:collection';
|
||||
import 'dart:io';
|
||||
import 'dart:math';
|
||||
import 'package:combinator/combinator.dart';
|
||||
import 'package:string_scanner/string_scanner.dart';
|
||||
import 'package:tuple/tuple.dart';
|
||||
|
||||
void main() {
|
||||
var expr = reference();
|
||||
var symbols = <String, dynamic>{};
|
||||
|
||||
void registerFunction(String name, int nArgs, Function(List<num>) f) {
|
||||
symbols[name] = new Tuple2(nArgs, f);
|
||||
}
|
||||
|
||||
registerFunction('**', 2, (args) => pow(args[0], args[1]));
|
||||
registerFunction('*', 2, (args) => args[0] * args[1]);
|
||||
registerFunction('/', 2, (args) => args[0] / args[1]);
|
||||
registerFunction('%', 2, (args) => args[0] % args[1]);
|
||||
registerFunction('+', 2, (args) => args[0] + args[1]);
|
||||
registerFunction('-', 2, (args) => args[0] - args[1]);
|
||||
registerFunction('.', 1, (args) => args[0].toDouble());
|
||||
registerFunction('print', 1, (args) {
|
||||
print(args[0]);
|
||||
return args[0];
|
||||
});
|
||||
|
||||
var number = match(new RegExp(r'[0-9]+(\.[0-9]+)?'),
|
||||
errorMessage: 'Expected a number.')
|
||||
.map((r) => num.parse(r.span.text));
|
||||
|
||||
var id = match(
|
||||
new RegExp(
|
||||
r'[A-Za-z_!\\$",\\+-\\./:;\\?<>%&\\*@\[\]\\{\}\\|`\\^~][A-Za-z0-9_!\\$",\\+-\\./:;\\?<>%&\*@\[\]\\{\}\\|`\\^~]*'),
|
||||
errorMessage: 'Expected an ID')
|
||||
.map((r) =>
|
||||
symbols[r.span.text] ??= throw "Undefined symbol: '${r.span.text}'");
|
||||
|
||||
var atom = number.castDynamic().or(id);
|
||||
|
||||
var list = expr.space().times(2, exact: false).map((r) {
|
||||
try {
|
||||
var out = [];
|
||||
var q = new Queue.from(r.value.reversed);
|
||||
|
||||
while (q.isNotEmpty) {
|
||||
var current = q.removeFirst();
|
||||
if (current is! Tuple2)
|
||||
out.insert(0, current);
|
||||
else {
|
||||
var args = [];
|
||||
for (int i = 0; i < (current.item1 as num); i++)
|
||||
args.add(out.removeLast());
|
||||
out.add(current.item2(args));
|
||||
}
|
||||
}
|
||||
|
||||
return out.length == 1 ? out.first : out;
|
||||
} catch (_) {
|
||||
return [];
|
||||
}
|
||||
});
|
||||
|
||||
expr.parser = longest([
|
||||
list,
|
||||
atom,
|
||||
expr.parenthesized(),
|
||||
]); //list | atom | expr.parenthesized();
|
||||
|
||||
while (true) {
|
||||
stdout.write('> ');
|
||||
var line = stdin.readLineSync();
|
||||
var result = expr.parse(new SpanScanner(line));
|
||||
|
||||
if (result.errors.isNotEmpty) {
|
||||
for (var error in result.errors) {
|
||||
print(error.toolString);
|
||||
print(error.message);
|
||||
}
|
||||
} else {
|
||||
print(result.value);
|
||||
}
|
||||
}
|
||||
}
|
14
packages/combinator/example/tuple.dart
Normal file
14
packages/combinator/example/tuple.dart
Normal file
|
@ -0,0 +1,14 @@
|
|||
import 'package:combinator/combinator.dart';
|
||||
import 'package:string_scanner/string_scanner.dart';
|
||||
|
||||
void main() {
|
||||
var pub = match('pub').map((r) => r.span.text).space();
|
||||
var dart = match('dart').map((r) => 24).space();
|
||||
var lang = match('lang').map((r) => true).space();
|
||||
|
||||
// Parses a Tuple3<String, int, bool>
|
||||
var grammar = tuple3(pub, dart, lang);
|
||||
|
||||
var scanner = SpanScanner('pub dart lang');
|
||||
print(grammar.parse(scanner).value);
|
||||
}
|
2
packages/combinator/lib/combinator.dart
Normal file
2
packages/combinator/lib/combinator.dart
Normal file
|
@ -0,0 +1,2 @@
|
|||
export 'src/combinator/combinator.dart';
|
||||
export 'src/error.dart';
|
26
packages/combinator/lib/src/combinator/advance.dart
Normal file
26
packages/combinator/lib/src/combinator/advance.dart
Normal file
|
@ -0,0 +1,26 @@
|
|||
part of lex.src.combinator;
|
||||
|
||||
class _Advance<T> extends Parser<T> {
|
||||
final Parser<T> parser;
|
||||
final int amount;
|
||||
|
||||
_Advance(this.parser, this.amount);
|
||||
|
||||
@override
|
||||
ParseResult<T> __parse(ParseArgs args) {
|
||||
var result = parser._parse(args.increaseDepth()).change(parser: this);
|
||||
if (result.successful) args.scanner.position += amount;
|
||||
return result;
|
||||
}
|
||||
|
||||
@override
|
||||
void stringify(CodeBuffer buffer) {
|
||||
buffer
|
||||
..writeln('advance($amount) (')
|
||||
..indent();
|
||||
parser.stringify(buffer);
|
||||
buffer
|
||||
..outdent()
|
||||
..writeln(')');
|
||||
}
|
||||
}
|
85
packages/combinator/lib/src/combinator/any.dart
Normal file
85
packages/combinator/lib/src/combinator/any.dart
Normal file
|
@ -0,0 +1,85 @@
|
|||
part of lex.src.combinator;
|
||||
|
||||
/// Matches any one of the given [parsers].
|
||||
///
|
||||
/// If [backtrack] is `true` (default), a failed parse will not modify the scanner state.
|
||||
///
|
||||
/// You can provide a custom [errorMessage]. You can set it to `false` to not
|
||||
/// generate any error at all.
|
||||
Parser<T> any<T>(Iterable<Parser<T>> parsers,
|
||||
{bool backtrack: true, errorMessage, SyntaxErrorSeverity severity}) {
|
||||
return new _Any(parsers, backtrack != false, errorMessage,
|
||||
severity ?? SyntaxErrorSeverity.error);
|
||||
}
|
||||
|
||||
class _Any<T> extends Parser<T> {
|
||||
final Iterable<Parser<T>> parsers;
|
||||
final bool backtrack;
|
||||
final errorMessage;
|
||||
final SyntaxErrorSeverity severity;
|
||||
|
||||
_Any(this.parsers, this.backtrack, this.errorMessage, this.severity);
|
||||
|
||||
@override
|
||||
ParseResult<T> _parse(ParseArgs args) {
|
||||
var inactive = parsers
|
||||
.where((p) => !args.trampoline.isActive(p, args.scanner.position));
|
||||
|
||||
if (inactive.isEmpty) {
|
||||
return new ParseResult(args.trampoline, args.scanner, this, false, []);
|
||||
}
|
||||
|
||||
var errors = <SyntaxError>[];
|
||||
int replay = args.scanner.position;
|
||||
|
||||
for (var parser in inactive) {
|
||||
var result = parser._parse(args.increaseDepth());
|
||||
|
||||
if (result.successful)
|
||||
return result;
|
||||
else {
|
||||
if (backtrack) args.scanner.position = replay;
|
||||
if (parser is _Alt) errors.addAll(result.errors);
|
||||
}
|
||||
}
|
||||
|
||||
if (errorMessage != false) {
|
||||
errors.add(
|
||||
new SyntaxError(
|
||||
severity,
|
||||
errorMessage?.toString() ??
|
||||
'No match found for ${parsers.length} alternative(s)',
|
||||
args.scanner.emptySpan,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return new ParseResult(args.trampoline, args.scanner, this, false, errors);
|
||||
}
|
||||
|
||||
@override
|
||||
ParseResult<T> __parse(ParseArgs args) {
|
||||
// Never called
|
||||
return null;
|
||||
}
|
||||
|
||||
@override
|
||||
void stringify(CodeBuffer buffer) {
|
||||
buffer
|
||||
..writeln('any(${parsers.length}) (')
|
||||
..indent();
|
||||
int i = 1;
|
||||
|
||||
for (var parser in parsers) {
|
||||
buffer
|
||||
..writeln('#${i++}:')
|
||||
..indent();
|
||||
parser.stringify(buffer);
|
||||
buffer.outdent();
|
||||
}
|
||||
|
||||
buffer
|
||||
..outdent()
|
||||
..writeln(')');
|
||||
}
|
||||
}
|
26
packages/combinator/lib/src/combinator/cache.dart
Normal file
26
packages/combinator/lib/src/combinator/cache.dart
Normal file
|
@ -0,0 +1,26 @@
|
|||
part of lex.src.combinator;
|
||||
|
||||
class _Cache<T> extends Parser<T> {
|
||||
final Map<int, ParseResult<T>> _cache = {};
|
||||
final Parser<T> parser;
|
||||
|
||||
_Cache(this.parser);
|
||||
|
||||
@override
|
||||
ParseResult<T> __parse(ParseArgs args) {
|
||||
return _cache.putIfAbsent(args.scanner.position, () {
|
||||
return parser._parse(args.increaseDepth());
|
||||
}).change(parser: this);
|
||||
}
|
||||
|
||||
@override
|
||||
void stringify(CodeBuffer buffer) {
|
||||
buffer
|
||||
..writeln('cache(${_cache.length}) (')
|
||||
..indent();
|
||||
parser.stringify(buffer);
|
||||
buffer
|
||||
..outdent()
|
||||
..writeln(')');
|
||||
}
|
||||
}
|
63
packages/combinator/lib/src/combinator/cast.dart
Normal file
63
packages/combinator/lib/src/combinator/cast.dart
Normal file
|
@ -0,0 +1,63 @@
|
|||
part of lex.src.combinator;
|
||||
|
||||
class _Cast<T, U extends T> extends Parser<U> {
|
||||
final Parser<T> parser;
|
||||
|
||||
_Cast(this.parser);
|
||||
|
||||
@override
|
||||
ParseResult<U> __parse(ParseArgs args) {
|
||||
var result = parser._parse(args.increaseDepth());
|
||||
return new ParseResult<U>(
|
||||
args.trampoline,
|
||||
args.scanner,
|
||||
this,
|
||||
result.successful,
|
||||
result.errors,
|
||||
span: result.span,
|
||||
value: result.value as U,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void stringify(CodeBuffer buffer) {
|
||||
buffer
|
||||
..writeln('cast<$U> (')
|
||||
..indent();
|
||||
parser.stringify(buffer);
|
||||
buffer
|
||||
..outdent()
|
||||
..writeln(')');
|
||||
}
|
||||
}
|
||||
|
||||
class _CastDynamic<T> extends Parser<dynamic> {
|
||||
final Parser<T> parser;
|
||||
|
||||
_CastDynamic(this.parser);
|
||||
|
||||
@override
|
||||
ParseResult<dynamic> __parse(ParseArgs args) {
|
||||
var result = parser._parse(args.increaseDepth());
|
||||
return new ParseResult<dynamic>(
|
||||
args.trampoline,
|
||||
args.scanner,
|
||||
this,
|
||||
result.successful,
|
||||
result.errors,
|
||||
span: result.span,
|
||||
value: result.value,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void stringify(CodeBuffer buffer) {
|
||||
buffer
|
||||
..writeln('cast<dynamic> (')
|
||||
..indent();
|
||||
parser.stringify(buffer);
|
||||
buffer
|
||||
..outdent()
|
||||
..writeln(')');
|
||||
}
|
||||
}
|
105
packages/combinator/lib/src/combinator/chain.dart
Normal file
105
packages/combinator/lib/src/combinator/chain.dart
Normal file
|
@ -0,0 +1,105 @@
|
|||
part of lex.src.combinator;
|
||||
|
||||
/// Expects to parse a sequence of [parsers].
|
||||
///
|
||||
/// If [failFast] is `true` (default), then the first failure to parse will abort the parse.
|
||||
ListParser<T> chain<T>(Iterable<Parser<T>> parsers,
|
||||
{bool failFast: true, SyntaxErrorSeverity severity}) {
|
||||
return new _Chain<T>(
|
||||
parsers, failFast != false, severity ?? SyntaxErrorSeverity.error);
|
||||
}
|
||||
|
||||
class _Alt<T> extends Parser<T> {
|
||||
final Parser<T> parser;
|
||||
final String errorMessage;
|
||||
final SyntaxErrorSeverity severity;
|
||||
|
||||
_Alt(this.parser, this.errorMessage, this.severity);
|
||||
|
||||
@override
|
||||
ParseResult<T> __parse(ParseArgs args) {
|
||||
var result = parser._parse(args.increaseDepth());
|
||||
return result.successful
|
||||
? result
|
||||
: result.addErrors([
|
||||
new SyntaxError(severity ?? SyntaxErrorSeverity.error, errorMessage,
|
||||
result.span ?? args.scanner.emptySpan),
|
||||
]);
|
||||
}
|
||||
|
||||
@override
|
||||
void stringify(CodeBuffer buffer) {
|
||||
parser.stringify(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
class _Chain<T> extends ListParser<T> {
|
||||
final Iterable<Parser<T>> parsers;
|
||||
final bool failFast;
|
||||
final SyntaxErrorSeverity severity;
|
||||
|
||||
_Chain(this.parsers, this.failFast, this.severity);
|
||||
|
||||
@override
|
||||
ParseResult<List<T>> __parse(ParseArgs args) {
|
||||
var errors = <SyntaxError>[];
|
||||
var results = <T>[];
|
||||
var spans = <FileSpan>[];
|
||||
bool successful = true;
|
||||
|
||||
for (var parser in parsers) {
|
||||
var result = parser._parse(args.increaseDepth());
|
||||
|
||||
if (!result.successful) {
|
||||
if (parser is _Alt) errors.addAll(result.errors);
|
||||
|
||||
if (failFast) {
|
||||
return new ParseResult(
|
||||
args.trampoline, args.scanner, this, false, result.errors);
|
||||
}
|
||||
|
||||
successful = false;
|
||||
}
|
||||
|
||||
results.add(result.value);
|
||||
|
||||
if (result.span != null) spans.add(result.span);
|
||||
}
|
||||
|
||||
FileSpan span;
|
||||
|
||||
if (spans.isNotEmpty) {
|
||||
span = spans.reduce((a, b) => a.expand(b));
|
||||
}
|
||||
|
||||
return new ParseResult<List<T>>(
|
||||
args.trampoline,
|
||||
args.scanner,
|
||||
this,
|
||||
successful,
|
||||
errors,
|
||||
span: span,
|
||||
value: new List<T>.unmodifiable(results),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void stringify(CodeBuffer buffer) {
|
||||
buffer
|
||||
..writeln('chain(${parsers.length}) (')
|
||||
..indent();
|
||||
int i = 1;
|
||||
|
||||
for (var parser in parsers) {
|
||||
buffer
|
||||
..writeln('#${i++}:')
|
||||
..indent();
|
||||
parser.stringify(buffer);
|
||||
buffer.outdent();
|
||||
}
|
||||
|
||||
buffer
|
||||
..outdent()
|
||||
..writeln(')');
|
||||
}
|
||||
}
|
42
packages/combinator/lib/src/combinator/check.dart
Normal file
42
packages/combinator/lib/src/combinator/check.dart
Normal file
|
@ -0,0 +1,42 @@
|
|||
part of lex.src.combinator;
|
||||
|
||||
class _Check<T> extends Parser<T> {
|
||||
final Parser<T> parser;
|
||||
final Matcher matcher;
|
||||
final String errorMessage;
|
||||
final SyntaxErrorSeverity severity;
|
||||
|
||||
_Check(this.parser, this.matcher, this.errorMessage, this.severity);
|
||||
|
||||
@override
|
||||
ParseResult<T> __parse(ParseArgs args) {
|
||||
var matchState = {};
|
||||
var result = parser._parse(args.increaseDepth()).change(parser: this);
|
||||
if (!result.successful)
|
||||
return result;
|
||||
else if (!matcher.matches(result.value, matchState)) {
|
||||
return result.change(successful: false).addErrors([
|
||||
new SyntaxError(
|
||||
severity,
|
||||
errorMessage ??
|
||||
matcher.describe(new StringDescription('Expected ')).toString() +
|
||||
'.',
|
||||
result.span,
|
||||
),
|
||||
]);
|
||||
} else
|
||||
return result;
|
||||
}
|
||||
|
||||
@override
|
||||
void stringify(CodeBuffer buffer) {
|
||||
var d = matcher.describe(new StringDescription());
|
||||
buffer
|
||||
..writeln('check($d) (')
|
||||
..indent();
|
||||
parser.stringify(buffer);
|
||||
buffer
|
||||
..outdent()
|
||||
..writeln(')');
|
||||
}
|
||||
}
|
381
packages/combinator/lib/src/combinator/combinator.dart
Normal file
381
packages/combinator/lib/src/combinator/combinator.dart
Normal file
|
@ -0,0 +1,381 @@
|
|||
library lex.src.combinator;
|
||||
|
||||
import 'dart:collection';
|
||||
|
||||
import 'package:code_buffer/code_buffer.dart';
|
||||
import 'package:matcher/matcher.dart';
|
||||
import 'package:source_span/source_span.dart';
|
||||
import 'package:string_scanner/string_scanner.dart';
|
||||
import 'package:tuple/tuple.dart';
|
||||
import '../error.dart';
|
||||
|
||||
part 'any.dart';
|
||||
|
||||
part 'advance.dart';
|
||||
|
||||
part 'cache.dart';
|
||||
|
||||
part 'cast.dart';
|
||||
|
||||
part 'chain.dart';
|
||||
|
||||
part 'check.dart';
|
||||
|
||||
part 'compare.dart';
|
||||
|
||||
part 'fold_errors.dart';
|
||||
|
||||
part 'index.dart';
|
||||
|
||||
part 'longest.dart';
|
||||
|
||||
part 'map.dart';
|
||||
|
||||
part 'match.dart';
|
||||
|
||||
part 'max_depth.dart';
|
||||
|
||||
part 'negate.dart';
|
||||
|
||||
part 'opt.dart';
|
||||
|
||||
part 'recursion.dart';
|
||||
|
||||
part 'reduce.dart';
|
||||
|
||||
part 'reference.dart';
|
||||
|
||||
part 'repeat.dart';
|
||||
|
||||
part 'safe.dart';
|
||||
|
||||
part 'to_list.dart';
|
||||
|
||||
part 'util.dart';
|
||||
|
||||
part 'value.dart';
|
||||
|
||||
class ParseArgs {
|
||||
final Trampoline trampoline;
|
||||
final SpanScanner scanner;
|
||||
final int depth;
|
||||
|
||||
ParseArgs(this.trampoline, this.scanner, this.depth);
|
||||
|
||||
ParseArgs increaseDepth() => new ParseArgs(trampoline, scanner, depth + 1);
|
||||
}
|
||||
|
||||
/// A parser combinator, which can parse very complicated grammars in a manageable manner.
|
||||
abstract class Parser<T> {
|
||||
ParseResult<T> __parse(ParseArgs args);
|
||||
|
||||
ParseResult<T> _parse(ParseArgs args) {
|
||||
var pos = args.scanner.position;
|
||||
|
||||
if (args.trampoline.hasMemoized(this, pos))
|
||||
return args.trampoline.getMemoized<T>(this, pos);
|
||||
|
||||
if (args.trampoline.isActive(this, pos))
|
||||
return new ParseResult(args.trampoline, args.scanner, this, false, []);
|
||||
|
||||
args.trampoline.enter(this, pos);
|
||||
var result = __parse(args);
|
||||
args.trampoline.memoize(this, pos, result);
|
||||
args.trampoline.exit(this);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Parses text from a [SpanScanner].
|
||||
ParseResult<T> parse(SpanScanner scanner, [int depth = 1]) {
|
||||
var args = new ParseArgs(new Trampoline(), scanner, depth);
|
||||
return _parse(args);
|
||||
}
|
||||
|
||||
/// Skips forward a certain amount of steps after parsing, if it was successful.
|
||||
Parser<T> forward(int amount) => new _Advance<T>(this, amount);
|
||||
|
||||
/// Moves backward a certain amount of steps after parsing, if it was successful.
|
||||
Parser<T> back(int amount) => new _Advance<T>(this, amount * -1);
|
||||
|
||||
/// Casts this parser to produce [U] objects.
|
||||
Parser<U> cast<U extends T>() => new _Cast<T, U>(this);
|
||||
|
||||
/// Casts this parser to produce [dynamic] objects.
|
||||
Parser<dynamic> castDynamic() => new _CastDynamic<T>(this);
|
||||
|
||||
/// Runs the given function, which changes the returned [ParseResult] into one relating to a [U] object.
|
||||
Parser<U> change<U>(ParseResult<U> Function(ParseResult<T>) f) {
|
||||
return new _Change<T, U>(this, f);
|
||||
}
|
||||
|
||||
/// Validates the parse result against a [Matcher].
|
||||
///
|
||||
/// You can provide a custom [errorMessage].
|
||||
Parser<T> check(Matcher matcher,
|
||||
{String errorMessage, SyntaxErrorSeverity severity}) =>
|
||||
new _Check<T>(
|
||||
this, matcher, errorMessage, severity ?? SyntaxErrorSeverity.error);
|
||||
|
||||
/// Binds an [errorMessage] to a copy of this parser.
|
||||
Parser<T> error({String errorMessage, SyntaxErrorSeverity severity}) =>
|
||||
new _Alt<T>(this, errorMessage, severity ?? SyntaxErrorSeverity.error);
|
||||
|
||||
/// Removes multiple errors that occur in the same spot; this can reduce noise in parser output.
|
||||
Parser<T> foldErrors({bool equal(SyntaxError a, SyntaxError b)}) {
|
||||
equal ??= (b, e) => b.span.start.offset == e.span.start.offset;
|
||||
return new _FoldErrors<T>(this, equal);
|
||||
}
|
||||
|
||||
/// Transforms the parse result using a unary function.
|
||||
Parser<U> map<U>(U Function(ParseResult<T>) f) {
|
||||
return new _Map<T, U>(this, f);
|
||||
}
|
||||
|
||||
/// Prevents recursion past a certain [depth], preventing stack overflow errors.
|
||||
Parser<T> maxDepth(int depth) => new _MaxDepth<T>(this, depth);
|
||||
|
||||
Parser<T> operator ~() => negate();
|
||||
|
||||
/// Ensures this pattern is not matched.
|
||||
///
|
||||
/// You can provide an [errorMessage].
|
||||
Parser<T> negate({String errorMessage, SyntaxErrorSeverity severity}) =>
|
||||
new _Negate<T>(this, errorMessage, severity ?? SyntaxErrorSeverity.error);
|
||||
|
||||
/// Caches the results of parse attempts at various locations within the source text.
|
||||
///
|
||||
/// Use this to prevent excessive recursion.
|
||||
Parser<T> cache() => new _Cache<T>(this);
|
||||
|
||||
Parser<T> operator &(Parser<T> other) => and(other);
|
||||
|
||||
/// Consumes `this` and another parser, but only considers the result of `this` parser.
|
||||
Parser<T> and(Parser other) => then(other).change<T>((r) {
|
||||
return new ParseResult(
|
||||
r.trampoline,
|
||||
r.scanner,
|
||||
this,
|
||||
r.successful,
|
||||
r.errors,
|
||||
span: r.span,
|
||||
value: (r.value != null ? r.value[0] : r.value) as T,
|
||||
);
|
||||
});
|
||||
|
||||
Parser<T> operator |(Parser<T> other) => or(other);
|
||||
|
||||
/// Shortcut for [or]-ing two parsers.
|
||||
Parser<T> or<U>(Parser<T> other) => any<T>([this, other]);
|
||||
|
||||
/// Parses this sequence one or more times.
|
||||
ListParser<T> plus() => times(1, exact: false);
|
||||
|
||||
/// Safely escapes this parser when an error occurs.
|
||||
///
|
||||
/// The generated parser only runs once; repeated uses always exit eagerly.
|
||||
Parser<T> safe(
|
||||
{bool backtrack: true,
|
||||
String errorMessage,
|
||||
SyntaxErrorSeverity severity}) =>
|
||||
new _Safe<T>(
|
||||
this, backtrack, errorMessage, severity ?? SyntaxErrorSeverity.error);
|
||||
|
||||
Parser<List<T>> separatedByComma() =>
|
||||
separatedBy(match<List<T>>(',').space());
|
||||
|
||||
/// Expects to see an infinite amounts of the pattern, separated by the [other] pattern.
|
||||
///
|
||||
/// Use this as a shortcut to parse arrays, parameter lists, etc.
|
||||
Parser<List<T>> separatedBy(Parser other) {
|
||||
var suffix = other.then(this).index(1).cast<T>();
|
||||
return this.then(suffix.star()).map((r) {
|
||||
var preceding =
|
||||
r.value.isEmpty ? [] : (r.value[0] == null ? [] : [r.value[0]]);
|
||||
var out = new List<T>.from(preceding);
|
||||
if (r.value[1] != null) out.addAll(r.value[1] as Iterable<T>);
|
||||
return out;
|
||||
});
|
||||
}
|
||||
|
||||
Parser<T> surroundedByCurlyBraces({T defaultValue}) => opt()
|
||||
.surroundedBy(match('{').space(), match('}').space())
|
||||
.map((r) => r.value ?? defaultValue);
|
||||
|
||||
Parser<T> surroundedBySquareBrackets({T defaultValue}) => opt()
|
||||
.surroundedBy(match('[').space(), match(']').space())
|
||||
.map((r) => r.value ?? defaultValue);
|
||||
|
||||
/// Expects to see the pattern, surrounded by the others.
|
||||
///
|
||||
/// If no [right] is provided, it expects to see the same pattern on both sides.
|
||||
/// Use this parse things like parenthesized expressions, arrays, etc.
|
||||
Parser<T> surroundedBy(Parser left, [Parser right]) {
|
||||
return chain([
|
||||
left,
|
||||
this,
|
||||
right ?? left,
|
||||
]).index(1).castDynamic().cast<T>();
|
||||
}
|
||||
|
||||
/// Parses `this`, either as-is or wrapped in parentheses.
|
||||
Parser<T> maybeParenthesized() {
|
||||
return any([parenthesized(), this]);
|
||||
}
|
||||
|
||||
/// Parses `this`, wrapped in parentheses.
|
||||
Parser<T> parenthesized() =>
|
||||
surroundedBy(match('(').space(), match(')').space());
|
||||
|
||||
/// Consumes any trailing whitespace.
|
||||
Parser<T> space() => trail(new RegExp(r'[ \n\r\t]+'));
|
||||
|
||||
/// Consumes 0 or more instance(s) of this parser.
|
||||
ListParser<T> star({bool backtrack: true}) =>
|
||||
times(1, exact: false, backtrack: backtrack).opt();
|
||||
|
||||
/// Shortcut for [chain]-ing two parsers together.
|
||||
ListParser<dynamic> then(Parser other) => chain<dynamic>([this, other]);
|
||||
|
||||
/// Casts this instance into a [ListParser].
|
||||
ListParser<T> toList() => new _ToList<T>(this);
|
||||
|
||||
/// Consumes and ignores any trailing occurrences of [pattern].
|
||||
Parser<T> trail(Pattern pattern) =>
|
||||
then(match(pattern).opt()).first().cast<T>();
|
||||
|
||||
/// Expect this pattern a certain number of times.
|
||||
///
|
||||
/// If [exact] is `false` (default: `true`), then the generated parser will accept
|
||||
/// an infinite amount of occurrences after the specified [count].
|
||||
///
|
||||
/// You can provide custom error messages for when there are [tooFew] or [tooMany] occurrences.
|
||||
ListParser<T> times(int count,
|
||||
{bool exact: true,
|
||||
String tooFew,
|
||||
String tooMany,
|
||||
bool backtrack: true,
|
||||
SyntaxErrorSeverity severity}) {
|
||||
return new _Repeat<T>(this, count, exact, tooFew, tooMany, backtrack,
|
||||
severity ?? SyntaxErrorSeverity.error);
|
||||
}
|
||||
|
||||
/// Produces an optional copy of this parser.
|
||||
///
|
||||
/// If [backtrack] is `true` (default), then a failed parse will not
|
||||
/// modify the scanner state.
|
||||
Parser<T> opt({bool backtrack: true}) => new _Opt(this, backtrack);
|
||||
|
||||
/// Sets the value of the [ParseResult].
|
||||
Parser<T> value(T Function(ParseResult<T>) f) {
|
||||
return new _Value<T>(this, f);
|
||||
}
|
||||
|
||||
/// Prints a representation of this parser, ideally without causing a stack overflow.
|
||||
void stringify(CodeBuffer buffer);
|
||||
}
|
||||
|
||||
/// A [Parser] that produces [List]s of a type [T].
|
||||
abstract class ListParser<T> extends Parser<List<T>> {
|
||||
/// Shortcut for calling [index] with `0`.
|
||||
Parser<T> first() => index(0);
|
||||
|
||||
/// Modifies this parser to only return the value at the given index [i].
|
||||
Parser<T> index(int i) => new _Index<T>(this, i);
|
||||
|
||||
/// Shortcut for calling [index] with the greatest-possible index.
|
||||
Parser<T> last() => index(-1);
|
||||
|
||||
/// Modifies this parser to call `List.reduce` on the parsed values.
|
||||
Parser<T> reduce(T Function(T, T) combine) => new _Reduce<T>(this, combine);
|
||||
|
||||
/// Sorts the parsed values, using the given [Comparator].
|
||||
ListParser<T> sort(Comparator<T> compare) => new _Compare(this, compare);
|
||||
|
||||
@override
|
||||
ListParser<T> opt({bool backtrack: true}) => new _ListOpt(this, backtrack);
|
||||
|
||||
/// Modifies this parser, returning only the values that match a predicate.
|
||||
Parser<List<T>> where(bool Function(T) f) =>
|
||||
map<List<T>>((r) => r.value.where(f).toList());
|
||||
|
||||
/// Condenses a [ListParser] into having a value of the combined span's text.
|
||||
Parser<String> flatten() => map<String>((r) => r.span.text);
|
||||
}
|
||||
|
||||
/// Prevents stack overflow in recursive parsers.
|
||||
class Trampoline {
|
||||
final Map<Parser, Queue<int>> _active = {};
|
||||
final Map<Parser, List<Tuple2<int, ParseResult>>> _memo = {};
|
||||
|
||||
bool hasMemoized(Parser parser, int position) {
|
||||
var list = _memo[parser];
|
||||
return list?.any((t) => t.item1 == position) == true;
|
||||
}
|
||||
|
||||
ParseResult<T> getMemoized<T>(Parser parser, int position) {
|
||||
return _memo[parser].firstWhere((t) => t.item1 == position).item2
|
||||
as ParseResult<T>;
|
||||
}
|
||||
|
||||
void memoize(Parser parser, int position, ParseResult result) {
|
||||
if (result != null) {
|
||||
var list = _memo.putIfAbsent(parser, () => []);
|
||||
var tuple = new Tuple2(position, result);
|
||||
if (!list.contains(tuple)) list.add(tuple);
|
||||
}
|
||||
}
|
||||
|
||||
bool isActive(Parser parser, int position) {
|
||||
if (!_active.containsKey(parser)) return false;
|
||||
var q = _active[parser];
|
||||
if (q.isEmpty) return false;
|
||||
//return q.contains(position);
|
||||
return q.first == position;
|
||||
}
|
||||
|
||||
void enter(Parser parser, int position) {
|
||||
_active.putIfAbsent(parser, () => new Queue()).addFirst(position);
|
||||
}
|
||||
|
||||
void exit(Parser parser) {
|
||||
if (_active.containsKey(parser)) _active[parser].removeFirst();
|
||||
}
|
||||
}
|
||||
|
||||
/// The result generated by a [Parser].
|
||||
class ParseResult<T> {
|
||||
final Parser<T> parser;
|
||||
final bool successful;
|
||||
final Iterable<SyntaxError> errors;
|
||||
final FileSpan span;
|
||||
final T value;
|
||||
final SpanScanner scanner;
|
||||
final Trampoline trampoline;
|
||||
|
||||
ParseResult(
|
||||
this.trampoline, this.scanner, this.parser, this.successful, this.errors,
|
||||
{this.span, this.value});
|
||||
|
||||
ParseResult<T> change(
|
||||
{Parser<T> parser,
|
||||
bool successful,
|
||||
Iterable<SyntaxError> errors,
|
||||
FileSpan span,
|
||||
T value}) {
|
||||
return new ParseResult<T>(
|
||||
trampoline,
|
||||
scanner,
|
||||
parser ?? this.parser,
|
||||
successful ?? this.successful,
|
||||
errors ?? this.errors,
|
||||
span: span ?? this.span,
|
||||
value: value ?? this.value,
|
||||
);
|
||||
}
|
||||
|
||||
ParseResult<T> addErrors(Iterable<SyntaxError> errors) {
|
||||
return change(
|
||||
errors: new List<SyntaxError>.from(this.errors)..addAll(errors),
|
||||
);
|
||||
}
|
||||
}
|
38
packages/combinator/lib/src/combinator/compare.dart
Normal file
38
packages/combinator/lib/src/combinator/compare.dart
Normal file
|
@ -0,0 +1,38 @@
|
|||
part of lex.src.combinator;
|
||||
|
||||
class _Compare<T> extends ListParser<T> {
|
||||
final ListParser<T> parser;
|
||||
final Comparator<T> compare;
|
||||
|
||||
_Compare(this.parser, this.compare);
|
||||
|
||||
@override
|
||||
ParseResult<List<T>> __parse(ParseArgs args) {
|
||||
var result = parser._parse(args.increaseDepth());
|
||||
if (!result.successful) return result;
|
||||
|
||||
result = result.change(
|
||||
value: result.value?.isNotEmpty == true ? result.value : []);
|
||||
result = result.change(value: new List<T>.from(result.value));
|
||||
return new ParseResult<List<T>>(
|
||||
args.trampoline,
|
||||
args.scanner,
|
||||
this,
|
||||
true,
|
||||
[],
|
||||
span: result.span,
|
||||
value: result.value..sort(compare),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void stringify(CodeBuffer buffer) {
|
||||
buffer
|
||||
..writeln('sort($compare) (')
|
||||
..indent();
|
||||
parser.stringify(buffer);
|
||||
buffer
|
||||
..outdent()
|
||||
..writeln(')');
|
||||
}
|
||||
}
|
29
packages/combinator/lib/src/combinator/fold_errors.dart
Normal file
29
packages/combinator/lib/src/combinator/fold_errors.dart
Normal file
|
@ -0,0 +1,29 @@
|
|||
part of lex.src.combinator;
|
||||
|
||||
class _FoldErrors<T> extends Parser<T> {
|
||||
final Parser<T> parser;
|
||||
final bool Function(SyntaxError, SyntaxError) equal;
|
||||
|
||||
_FoldErrors(this.parser, this.equal);
|
||||
|
||||
@override
|
||||
ParseResult<T> __parse(ParseArgs args) {
|
||||
var result = parser._parse(args.increaseDepth()).change(parser: this);
|
||||
var errors = result.errors.fold<List<SyntaxError>>([], (out, e) {
|
||||
if (!out.any((b) => equal(e, b))) out.add(e);
|
||||
return out;
|
||||
});
|
||||
return result.change(errors: errors);
|
||||
}
|
||||
|
||||
@override
|
||||
void stringify(CodeBuffer buffer) {
|
||||
buffer
|
||||
..writeln('fold errors (')
|
||||
..indent();
|
||||
parser.stringify(buffer);
|
||||
buffer
|
||||
..outdent()
|
||||
..writeln(')');
|
||||
}
|
||||
}
|
38
packages/combinator/lib/src/combinator/index.dart
Normal file
38
packages/combinator/lib/src/combinator/index.dart
Normal file
|
@ -0,0 +1,38 @@
|
|||
part of lex.src.combinator;
|
||||
|
||||
class _Index<T> extends Parser<T> {
|
||||
final ListParser<T> parser;
|
||||
final int index;
|
||||
|
||||
_Index(this.parser, this.index);
|
||||
|
||||
@override
|
||||
ParseResult<T> __parse(ParseArgs args) {
|
||||
var result = parser._parse(args.increaseDepth());
|
||||
Object value;
|
||||
|
||||
if (result.successful)
|
||||
value = index == -1 ? result.value.last : result.value.elementAt(index);
|
||||
|
||||
return new ParseResult<T>(
|
||||
args.trampoline,
|
||||
args.scanner,
|
||||
this,
|
||||
result.successful,
|
||||
result.errors,
|
||||
span: result.span,
|
||||
value: value as T,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void stringify(CodeBuffer buffer) {
|
||||
buffer
|
||||
..writeln('index($index) (')
|
||||
..indent();
|
||||
parser.stringify(buffer);
|
||||
buffer
|
||||
..outdent()
|
||||
..writeln(')');
|
||||
}
|
||||
}
|
115
packages/combinator/lib/src/combinator/longest.dart
Normal file
115
packages/combinator/lib/src/combinator/longest.dart
Normal file
|
@ -0,0 +1,115 @@
|
|||
part of lex.src.combinator;
|
||||
|
||||
/// Matches any one of the given [parsers].
|
||||
///
|
||||
/// You can provide a custom [errorMessage].
|
||||
Parser<T> longest<T>(Iterable<Parser<T>> parsers,
|
||||
{Object errorMessage, SyntaxErrorSeverity severity}) {
|
||||
return new _Longest(
|
||||
parsers, errorMessage, severity ?? SyntaxErrorSeverity.error);
|
||||
}
|
||||
|
||||
class _Longest<T> extends Parser<T> {
|
||||
final Iterable<Parser<T>> parsers;
|
||||
final Object errorMessage;
|
||||
final SyntaxErrorSeverity severity;
|
||||
|
||||
_Longest(this.parsers, this.errorMessage, this.severity);
|
||||
|
||||
@override
|
||||
ParseResult<T> _parse(ParseArgs args) {
|
||||
var inactive = parsers
|
||||
.toList()
|
||||
.where((p) => !args.trampoline.isActive(p, args.scanner.position));
|
||||
|
||||
if (inactive.isEmpty) {
|
||||
return new ParseResult(args.trampoline, args.scanner, this, false, []);
|
||||
}
|
||||
|
||||
int replay = args.scanner.position;
|
||||
var errors = <SyntaxError>[];
|
||||
var results = <ParseResult<T>>[];
|
||||
|
||||
for (var parser in inactive) {
|
||||
var result = parser._parse(args.increaseDepth());
|
||||
|
||||
if (result.successful && result.span != null)
|
||||
results.add(result);
|
||||
else if (parser is _Alt) errors.addAll(result.errors);
|
||||
|
||||
args.scanner.position = replay;
|
||||
}
|
||||
|
||||
if (results.isNotEmpty) {
|
||||
results.sort((a, b) => b.span.length.compareTo(a.span.length));
|
||||
args.scanner.scan(results.first.span.text);
|
||||
return results.first;
|
||||
}
|
||||
|
||||
if (errorMessage != false)
|
||||
errors.add(
|
||||
new SyntaxError(
|
||||
severity,
|
||||
errorMessage?.toString() ??
|
||||
'No match found for ${parsers.length} alternative(s)',
|
||||
args.scanner.emptySpan,
|
||||
),
|
||||
);
|
||||
|
||||
return new ParseResult(args.trampoline, args.scanner, this, false, errors);
|
||||
}
|
||||
|
||||
@override
|
||||
ParseResult<T> __parse(ParseArgs args) {
|
||||
int replay = args.scanner.position;
|
||||
var errors = <SyntaxError>[];
|
||||
var results = <ParseResult<T>>[];
|
||||
|
||||
for (var parser in parsers) {
|
||||
var result = parser._parse(args.increaseDepth());
|
||||
|
||||
if (result.successful)
|
||||
results.add(result);
|
||||
else if (parser is _Alt) errors.addAll(result.errors);
|
||||
|
||||
args.scanner.position = replay;
|
||||
}
|
||||
|
||||
if (results.isNotEmpty) {
|
||||
results.sort((a, b) => b.span.length.compareTo(a.span.length));
|
||||
args.scanner.scan(results.first.span.text);
|
||||
return results.first;
|
||||
}
|
||||
|
||||
errors.add(
|
||||
new SyntaxError(
|
||||
severity,
|
||||
errorMessage?.toString() ??
|
||||
'No match found for ${parsers.length} alternative(s)',
|
||||
args.scanner.emptySpan,
|
||||
),
|
||||
);
|
||||
|
||||
return new ParseResult(args.trampoline, args.scanner, this, false, errors);
|
||||
}
|
||||
|
||||
@override
|
||||
void stringify(CodeBuffer buffer) {
|
||||
buffer
|
||||
..writeln('longest(${parsers.length}) (')
|
||||
..indent();
|
||||
int i = 1;
|
||||
|
||||
for (var parser in parsers) {
|
||||
buffer
|
||||
..writeln('#${i++}:')
|
||||
..indent();
|
||||
parser.stringify(buffer);
|
||||
buffer.outdent();
|
||||
}
|
||||
|
||||
buffer
|
||||
..outdent()
|
||||
..writeln(')');
|
||||
}
|
||||
}
|
56
packages/combinator/lib/src/combinator/map.dart
Normal file
56
packages/combinator/lib/src/combinator/map.dart
Normal file
|
@ -0,0 +1,56 @@
|
|||
part of lex.src.combinator;
|
||||
|
||||
class _Map<T, U> extends Parser<U> {
|
||||
final Parser<T> parser;
|
||||
final U Function(ParseResult<T>) f;
|
||||
|
||||
_Map(this.parser, this.f);
|
||||
|
||||
@override
|
||||
ParseResult<U> __parse(ParseArgs args) {
|
||||
var result = parser._parse(args.increaseDepth());
|
||||
return new ParseResult<U>(
|
||||
args.trampoline,
|
||||
args.scanner,
|
||||
this,
|
||||
result.successful,
|
||||
result.errors,
|
||||
span: result.span,
|
||||
value: result.successful ? f(result) : null,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void stringify(CodeBuffer buffer) {
|
||||
buffer
|
||||
..writeln('map<$U> (')
|
||||
..indent();
|
||||
parser.stringify(buffer);
|
||||
buffer
|
||||
..outdent()
|
||||
..writeln(')');
|
||||
}
|
||||
}
|
||||
|
||||
class _Change<T, U> extends Parser<U> {
|
||||
final Parser<T> parser;
|
||||
final ParseResult<U> Function(ParseResult<T>) f;
|
||||
|
||||
_Change(this.parser, this.f);
|
||||
|
||||
@override
|
||||
ParseResult<U> __parse(ParseArgs args) {
|
||||
return f(parser._parse(args.increaseDepth())).change(parser: this);
|
||||
}
|
||||
|
||||
@override
|
||||
void stringify(CodeBuffer buffer) {
|
||||
buffer
|
||||
..writeln('change($f) (')
|
||||
..indent();
|
||||
parser.stringify(buffer);
|
||||
buffer
|
||||
..outdent()
|
||||
..writeln(')');
|
||||
}
|
||||
}
|
40
packages/combinator/lib/src/combinator/match.dart
Normal file
40
packages/combinator/lib/src/combinator/match.dart
Normal file
|
@ -0,0 +1,40 @@
|
|||
part of lex.src.combinator;
|
||||
|
||||
/// Expects to match a given [pattern]. If it is not matched, you can provide a custom [errorMessage].
|
||||
Parser<T> match<T>(Pattern pattern,
|
||||
{String errorMessage, SyntaxErrorSeverity severity}) =>
|
||||
new _Match<T>(pattern, errorMessage, severity ?? SyntaxErrorSeverity.error);
|
||||
|
||||
class _Match<T> extends Parser<T> {
|
||||
final Pattern pattern;
|
||||
final String errorMessage;
|
||||
final SyntaxErrorSeverity severity;
|
||||
|
||||
_Match(this.pattern, this.errorMessage, this.severity);
|
||||
|
||||
@override
|
||||
ParseResult<T> __parse(ParseArgs args) {
|
||||
var scanner = args.scanner;
|
||||
if (!scanner.scan(pattern))
|
||||
return new ParseResult(args.trampoline, scanner, this, false, [
|
||||
new SyntaxError(
|
||||
severity,
|
||||
errorMessage ?? 'Expected "$pattern".',
|
||||
scanner.emptySpan,
|
||||
),
|
||||
]);
|
||||
return new ParseResult<T>(
|
||||
args.trampoline,
|
||||
scanner,
|
||||
this,
|
||||
true,
|
||||
[],
|
||||
span: scanner.lastSpan,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void stringify(CodeBuffer buffer) {
|
||||
buffer.writeln('match($pattern)');
|
||||
}
|
||||
}
|
28
packages/combinator/lib/src/combinator/max_depth.dart
Normal file
28
packages/combinator/lib/src/combinator/max_depth.dart
Normal file
|
@ -0,0 +1,28 @@
|
|||
part of lex.src.combinator;
|
||||
|
||||
class _MaxDepth<T> extends Parser<T> {
|
||||
final Parser<T> parser;
|
||||
final int cap;
|
||||
|
||||
_MaxDepth(this.parser, this.cap);
|
||||
|
||||
@override
|
||||
ParseResult<T> __parse(ParseArgs args) {
|
||||
if (args.depth > cap) {
|
||||
return new ParseResult<T>(args.trampoline, args.scanner, this, false, []);
|
||||
}
|
||||
|
||||
return parser._parse(args.increaseDepth());
|
||||
}
|
||||
|
||||
@override
|
||||
void stringify(CodeBuffer buffer) {
|
||||
buffer
|
||||
..writeln('max depth($cap) (')
|
||||
..indent();
|
||||
parser.stringify(buffer);
|
||||
buffer
|
||||
..outdent()
|
||||
..writeln(')');
|
||||
}
|
||||
}
|
51
packages/combinator/lib/src/combinator/negate.dart
Normal file
51
packages/combinator/lib/src/combinator/negate.dart
Normal file
|
@ -0,0 +1,51 @@
|
|||
part of lex.src.combinator;
|
||||
|
||||
class _Negate<T> extends Parser<T> {
|
||||
final Parser<T> parser;
|
||||
final String errorMessage;
|
||||
final SyntaxErrorSeverity severity;
|
||||
|
||||
_Negate(this.parser, this.errorMessage, this.severity);
|
||||
|
||||
@override
|
||||
ParseResult<T> __parse(ParseArgs args) {
|
||||
var result = parser._parse(args.increaseDepth()).change(parser: this);
|
||||
|
||||
if (!result.successful) {
|
||||
return new ParseResult<T>(
|
||||
args.trampoline,
|
||||
args.scanner,
|
||||
this,
|
||||
true,
|
||||
[],
|
||||
span: result.span ?? args.scanner.lastSpan ?? args.scanner.emptySpan,
|
||||
value: result.value,
|
||||
);
|
||||
}
|
||||
|
||||
result = result.change(successful: false);
|
||||
|
||||
if (errorMessage != null) {
|
||||
result = result.addErrors([
|
||||
new SyntaxError(
|
||||
severity,
|
||||
errorMessage,
|
||||
result.span,
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@override
|
||||
void stringify(CodeBuffer buffer) {
|
||||
buffer
|
||||
..writeln('negate (')
|
||||
..indent();
|
||||
parser.stringify(buffer);
|
||||
buffer
|
||||
..outdent()
|
||||
..writeln(')');
|
||||
}
|
||||
}
|
57
packages/combinator/lib/src/combinator/opt.dart
Normal file
57
packages/combinator/lib/src/combinator/opt.dart
Normal file
|
@ -0,0 +1,57 @@
|
|||
part of lex.src.combinator;
|
||||
|
||||
class _Opt<T> extends Parser<T> {
|
||||
final Parser<T> parser;
|
||||
final bool backtrack;
|
||||
|
||||
_Opt(this.parser, this.backtrack);
|
||||
|
||||
@override
|
||||
ParseResult<T> __parse(ParseArgs args) {
|
||||
var replay = args.scanner.position;
|
||||
var result = parser._parse(args.increaseDepth());
|
||||
|
||||
if (!result.successful) args.scanner.position = replay;
|
||||
|
||||
return result.change(parser: this, successful: true);
|
||||
}
|
||||
|
||||
@override
|
||||
void stringify(CodeBuffer buffer) {
|
||||
buffer
|
||||
..writeln('optional (')
|
||||
..indent();
|
||||
parser.stringify(buffer);
|
||||
buffer
|
||||
..outdent()
|
||||
..writeln(')');
|
||||
}
|
||||
}
|
||||
|
||||
class _ListOpt<T> extends ListParser<T> {
|
||||
final ListParser<T> parser;
|
||||
final bool backtrack;
|
||||
|
||||
_ListOpt(this.parser, this.backtrack);
|
||||
|
||||
@override
|
||||
ParseResult<List<T>> __parse(ParseArgs args) {
|
||||
var replay = args.scanner.position;
|
||||
var result = parser._parse(args.increaseDepth());
|
||||
|
||||
if (!result.successful) args.scanner.position = replay;
|
||||
|
||||
return result.change(parser: this, successful: true);
|
||||
}
|
||||
|
||||
@override
|
||||
void stringify(CodeBuffer buffer) {
|
||||
buffer
|
||||
..writeln('optional (')
|
||||
..indent();
|
||||
parser.stringify(buffer);
|
||||
buffer
|
||||
..outdent()
|
||||
..writeln(')');
|
||||
}
|
||||
}
|
142
packages/combinator/lib/src/combinator/recursion.dart
Normal file
142
packages/combinator/lib/src/combinator/recursion.dart
Normal file
|
@ -0,0 +1,142 @@
|
|||
part of lex.src.combinator;
|
||||
|
||||
/*
|
||||
/// Handles left recursion in a grammar using the Pratt algorithm.
|
||||
class Recursion<T> {
|
||||
Iterable<Parser<T>> prefix;
|
||||
Map<Parser, T Function(T, T, ParseResult<T>)> infix;
|
||||
Map<Parser, T Function(T, T, ParseResult<T>)> postfix;
|
||||
|
||||
Recursion({this.prefix, this.infix, this.postfix}) {
|
||||
prefix ??= [];
|
||||
infix ??= {};
|
||||
postfix ??= {};
|
||||
}
|
||||
|
||||
Parser<T> precedence(int p) => new _Precedence(this, p);
|
||||
|
||||
void stringify(CodeBuffer buffer) {
|
||||
buffer
|
||||
..writeln('recursion (')
|
||||
..indent()
|
||||
..writeln('prefix(${prefix.length}')
|
||||
..writeln('infix(${infix.length}')
|
||||
..writeln('postfix(${postfix.length}')
|
||||
..outdent()
|
||||
..writeln(')');
|
||||
}
|
||||
}
|
||||
|
||||
class _Precedence<T> extends Parser<T> {
|
||||
final Recursion r;
|
||||
final int precedence;
|
||||
|
||||
_Precedence(this.r, this.precedence);
|
||||
|
||||
@override
|
||||
ParseResult<T> __parse(ParseArgs args) {
|
||||
int replay = args.scanner.position;
|
||||
var errors = <SyntaxError>[];
|
||||
var start = args.scanner.state;
|
||||
var reversedKeys = r.infix.keys.toList().reversed;
|
||||
|
||||
for (var pre in r.prefix) {
|
||||
var result = pre._parse(args.increaseDepth()), originalResult = result;
|
||||
|
||||
if (!result.successful) {
|
||||
if (pre is _Alt) errors.addAll(result.errors);
|
||||
args.scanner.position = replay;
|
||||
} else {
|
||||
var left = result.value;
|
||||
replay = args.scanner.position;
|
||||
//print('${result.span.text}:\n' + scanner.emptySpan.highlight());
|
||||
|
||||
while (true) {
|
||||
bool matched = false;
|
||||
|
||||
//for (int i = 0; i < r.infix.length; i++) {
|
||||
for (int i = r.infix.length - 1; i >= 0; i--) {
|
||||
//var fix = r.infix.keys.elementAt(r.infix.length - i - 1);
|
||||
var fix = reversedKeys.elementAt(i);
|
||||
|
||||
if (i < precedence) continue;
|
||||
|
||||
var result = fix._parse(args.increaseDepth());
|
||||
|
||||
if (!result.successful) {
|
||||
if (fix is _Alt) errors.addAll(result.errors);
|
||||
// If this is the last alternative and it failed, don't continue looping.
|
||||
//if (true || i + 1 < r.infix.length)
|
||||
args.scanner.position = replay;
|
||||
} else {
|
||||
//print('FOUND $fix when left was $left');
|
||||
//print('$i vs $precedence\n${originalResult.span.highlight()}');
|
||||
result = r.precedence(i)._parse(args.increaseDepth());
|
||||
|
||||
if (!result.successful) {
|
||||
} else {
|
||||
matched = false;
|
||||
var old = left;
|
||||
left = r.infix[fix](left, result.value, result);
|
||||
print(
|
||||
'$old $fix ${result.value} = $left\n${result.span.highlight()}');
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!matched) break;
|
||||
}
|
||||
|
||||
replay = args.scanner.position;
|
||||
//print('f ${result.span.text}');
|
||||
|
||||
for (var post in r.postfix.keys) {
|
||||
var result = pre._parse(args.increaseDepth());
|
||||
|
||||
if (!result.successful) {
|
||||
if (post is _Alt) errors.addAll(result.errors);
|
||||
args.scanner.position = replay;
|
||||
} else {
|
||||
left = r.infix[post](left, originalResult.value, result);
|
||||
}
|
||||
}
|
||||
|
||||
if (!args.scanner.isDone) {
|
||||
// If we're not done scanning, then we need some sort of guard to ensure the
|
||||
// that this exact parser does not run again in the exact position.
|
||||
}
|
||||
return new ParseResult(
|
||||
args.trampoline,
|
||||
args.scanner,
|
||||
this,
|
||||
true,
|
||||
errors,
|
||||
value: left,
|
||||
span: args.scanner.spanFrom(start),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return new ParseResult(
|
||||
args.trampoline,
|
||||
args.scanner,
|
||||
this,
|
||||
false,
|
||||
errors,
|
||||
span: args.scanner.spanFrom(start),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void stringify(CodeBuffer buffer) {
|
||||
buffer
|
||||
..writeln('precedence($precedence) (')
|
||||
..indent();
|
||||
r.stringify(buffer);
|
||||
buffer
|
||||
..outdent()
|
||||
..writeln(')');
|
||||
}
|
||||
}
|
||||
*/
|
45
packages/combinator/lib/src/combinator/reduce.dart
Normal file
45
packages/combinator/lib/src/combinator/reduce.dart
Normal file
|
@ -0,0 +1,45 @@
|
|||
part of lex.src.combinator;
|
||||
|
||||
class _Reduce<T> extends Parser<T> {
|
||||
final ListParser<T> parser;
|
||||
final T Function(T, T) combine;
|
||||
|
||||
_Reduce(this.parser, this.combine);
|
||||
|
||||
@override
|
||||
ParseResult<T> __parse(ParseArgs args) {
|
||||
var result = parser._parse(args.increaseDepth());
|
||||
|
||||
if (!result.successful)
|
||||
return new ParseResult<T>(
|
||||
args.trampoline,
|
||||
args.scanner,
|
||||
this,
|
||||
false,
|
||||
result.errors,
|
||||
);
|
||||
|
||||
result = result.change(
|
||||
value: result.value?.isNotEmpty == true ? result.value : []);
|
||||
return new ParseResult<T>(
|
||||
args.trampoline,
|
||||
args.scanner,
|
||||
this,
|
||||
result.successful,
|
||||
[],
|
||||
span: result.span,
|
||||
value: result.value.isEmpty ? null : result.value.reduce(combine),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void stringify(CodeBuffer buffer) {
|
||||
buffer
|
||||
..writeln('reduce($combine) (')
|
||||
..indent();
|
||||
parser.stringify(buffer);
|
||||
buffer
|
||||
..outdent()
|
||||
..writeln(')');
|
||||
}
|
||||
}
|
38
packages/combinator/lib/src/combinator/reference.dart
Normal file
38
packages/combinator/lib/src/combinator/reference.dart
Normal file
|
@ -0,0 +1,38 @@
|
|||
part of lex.src.combinator;
|
||||
|
||||
Reference<T> reference<T>() => new Reference<T>._();
|
||||
|
||||
class Reference<T> extends Parser<T> {
|
||||
Parser<T> _parser;
|
||||
bool printed = false;
|
||||
|
||||
Reference._();
|
||||
|
||||
void set parser(Parser<T> value) {
|
||||
if (_parser != null)
|
||||
throw new StateError(
|
||||
'There is already a parser assigned to this reference.');
|
||||
_parser = value;
|
||||
}
|
||||
|
||||
@override
|
||||
ParseResult<T> __parse(ParseArgs args) {
|
||||
if (_parser == null)
|
||||
throw new StateError('There is no parser assigned to this reference.');
|
||||
return _parser._parse(args);
|
||||
}
|
||||
|
||||
@override
|
||||
ParseResult<T> _parse(ParseArgs args) {
|
||||
return _parser._parse(args);
|
||||
}
|
||||
|
||||
@override
|
||||
void stringify(CodeBuffer buffer) {
|
||||
if (_parser == null)
|
||||
buffer.writeln('(undefined reference <$T>)');
|
||||
else if (!printed) _parser.stringify(buffer);
|
||||
printed = true;
|
||||
buffer.writeln('(previously printed reference)');
|
||||
}
|
||||
}
|
84
packages/combinator/lib/src/combinator/repeat.dart
Normal file
84
packages/combinator/lib/src/combinator/repeat.dart
Normal file
|
@ -0,0 +1,84 @@
|
|||
part of lex.src.combinator;
|
||||
|
||||
class _Repeat<T> extends ListParser<T> {
|
||||
final Parser<T> parser;
|
||||
final int count;
|
||||
final bool exact, backtrack;
|
||||
final String tooFew, tooMany;
|
||||
final SyntaxErrorSeverity severity;
|
||||
|
||||
_Repeat(this.parser, this.count, this.exact, this.tooFew, this.tooMany,
|
||||
this.backtrack, this.severity);
|
||||
|
||||
@override
|
||||
ParseResult<List<T>> __parse(ParseArgs args) {
|
||||
var errors = <SyntaxError>[];
|
||||
var results = <T>[];
|
||||
var spans = <FileSpan>[];
|
||||
int success = 0, replay = args.scanner.position;
|
||||
ParseResult<T> result;
|
||||
|
||||
do {
|
||||
result = parser._parse(args.increaseDepth());
|
||||
if (result.successful) {
|
||||
success++;
|
||||
results.add(result.value);
|
||||
replay = args.scanner.position;
|
||||
} else if (backtrack) args.scanner.position = replay;
|
||||
|
||||
if (result.span != null) spans.add(result.span);
|
||||
} while (result.successful);
|
||||
|
||||
if (success < count) {
|
||||
errors.addAll(result.errors);
|
||||
errors.add(
|
||||
new SyntaxError(
|
||||
severity,
|
||||
tooFew ?? 'Expected at least $count occurence(s).',
|
||||
result.span ?? args.scanner.emptySpan,
|
||||
),
|
||||
);
|
||||
|
||||
if (backtrack) args.scanner.position = replay;
|
||||
|
||||
return new ParseResult<List<T>>(
|
||||
args.trampoline, args.scanner, this, false, errors);
|
||||
} else if (success > count && exact) {
|
||||
if (backtrack) args.scanner.position = replay;
|
||||
|
||||
return new ParseResult<List<T>>(
|
||||
args.trampoline, args.scanner, this, false, [
|
||||
new SyntaxError(
|
||||
severity,
|
||||
tooMany ?? 'Expected no more than $count occurence(s).',
|
||||
result.span ?? args.scanner.emptySpan,
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
var span = spans.reduce((a, b) => a.expand(b));
|
||||
return new ParseResult<List<T>>(
|
||||
args.trampoline,
|
||||
args.scanner,
|
||||
this,
|
||||
true,
|
||||
[],
|
||||
span: span,
|
||||
value: results,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void stringify(CodeBuffer buffer) {
|
||||
var r = new StringBuffer('{$count');
|
||||
if (!exact) r.write(',');
|
||||
r.write('}');
|
||||
buffer
|
||||
..writeln('repeat($r) (')
|
||||
..indent();
|
||||
parser.stringify(buffer);
|
||||
buffer
|
||||
..outdent()
|
||||
..writeln(')');
|
||||
}
|
||||
}
|
51
packages/combinator/lib/src/combinator/safe.dart
Normal file
51
packages/combinator/lib/src/combinator/safe.dart
Normal file
|
@ -0,0 +1,51 @@
|
|||
part of lex.src.combinator;
|
||||
|
||||
class _Safe<T> extends Parser<T> {
|
||||
final Parser<T> parser;
|
||||
final bool backtrack;
|
||||
final String errorMessage;
|
||||
final SyntaxErrorSeverity severity;
|
||||
bool _triggered = false;
|
||||
|
||||
_Safe(this.parser, this.backtrack, this.errorMessage, this.severity);
|
||||
|
||||
@override
|
||||
ParseResult<T> __parse(ParseArgs args) {
|
||||
var replay = args.scanner.position;
|
||||
|
||||
try {
|
||||
if (_triggered) throw null;
|
||||
return parser._parse(args.increaseDepth());
|
||||
} catch (_) {
|
||||
_triggered = true;
|
||||
if (backtrack) args.scanner.position = replay;
|
||||
var errors = <SyntaxError>[];
|
||||
|
||||
if (errorMessage != null) {
|
||||
// TODO: Custom severity for all errors?
|
||||
errors.add(
|
||||
new SyntaxError(
|
||||
severity,
|
||||
errorMessage,
|
||||
args.scanner.lastSpan ?? args.scanner.emptySpan,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return new ParseResult<T>(
|
||||
args.trampoline, args.scanner, this, false, errors);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void stringify(CodeBuffer buffer) {
|
||||
var t = _triggered ? 'triggered' : 'not triggered';
|
||||
buffer
|
||||
..writeln('safe($t) (')
|
||||
..indent();
|
||||
parser.stringify(buffer);
|
||||
buffer
|
||||
..outdent()
|
||||
..writeln(')');
|
||||
}
|
||||
}
|
37
packages/combinator/lib/src/combinator/to_list.dart
Normal file
37
packages/combinator/lib/src/combinator/to_list.dart
Normal file
|
@ -0,0 +1,37 @@
|
|||
part of lex.src.combinator;
|
||||
|
||||
class _ToList<T> extends ListParser<T> {
|
||||
final Parser<T> parser;
|
||||
|
||||
_ToList(this.parser);
|
||||
|
||||
@override
|
||||
ParseResult<List<T>> __parse(ParseArgs args) {
|
||||
var result = parser._parse(args.increaseDepth());
|
||||
|
||||
if (result.value is List) {
|
||||
return (result as ParseResult<List<T>>).change(parser: this);
|
||||
}
|
||||
|
||||
return new ParseResult(
|
||||
args.trampoline,
|
||||
args.scanner,
|
||||
this,
|
||||
result.successful,
|
||||
result.errors,
|
||||
span: result.span,
|
||||
value: [result.value],
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void stringify(CodeBuffer buffer) {
|
||||
buffer
|
||||
..writeln('to list (')
|
||||
..indent();
|
||||
parser.stringify(buffer);
|
||||
buffer
|
||||
..outdent()
|
||||
..writeln(')');
|
||||
}
|
||||
}
|
57
packages/combinator/lib/src/combinator/util.dart
Normal file
57
packages/combinator/lib/src/combinator/util.dart
Normal file
|
@ -0,0 +1,57 @@
|
|||
part of lex.src.combinator;
|
||||
|
||||
/// A typed parser that parses a sequence of 2 values of different types.
|
||||
Parser<Tuple2<A, B>> tuple2<A, B>(Parser<A> a, Parser<B> b) {
|
||||
return chain([a, b]).map((r) {
|
||||
return Tuple2(r.value[0] as A, r.value[1] as B);
|
||||
});
|
||||
}
|
||||
|
||||
/// A typed parser that parses a sequence of 3 values of different types.
|
||||
Parser<Tuple3<A, B, C>> tuple3<A, B, C>(Parser<A> a, Parser<B> b, Parser<C> c) {
|
||||
return chain([a, b, c]).map((r) {
|
||||
return Tuple3(r.value[0] as A, r.value[1] as B, r.value[2] as C);
|
||||
});
|
||||
}
|
||||
|
||||
/// A typed parser that parses a sequence of 4 values of different types.
|
||||
Parser<Tuple4<A, B, C, D>> tuple4<A, B, C, D>(
|
||||
Parser<A> a, Parser<B> b, Parser<C> c, Parser<D> d) {
|
||||
return chain([a, b, c, d]).map((r) {
|
||||
return Tuple4(
|
||||
r.value[0] as A, r.value[1] as B, r.value[2] as C, r.value[3] as D);
|
||||
});
|
||||
}
|
||||
|
||||
/// A typed parser that parses a sequence of 5 values of different types.
|
||||
Parser<Tuple5<A, B, C, D, E>> tuple5<A, B, C, D, E>(
|
||||
Parser<A> a, Parser<B> b, Parser<C> c, Parser<D> d, Parser<E> e) {
|
||||
return chain([a, b, c, d, e]).map((r) {
|
||||
return Tuple5(r.value[0] as A, r.value[1] as B, r.value[2] as C,
|
||||
r.value[3] as D, r.value[4] as E);
|
||||
});
|
||||
}
|
||||
|
||||
/// A typed parser that parses a sequence of 6 values of different types.
|
||||
Parser<Tuple6<A, B, C, D, E, F>> tuple6<A, B, C, D, E, F>(Parser<A> a,
|
||||
Parser<B> b, Parser<C> c, Parser<D> d, Parser<E> e, Parser<F> f) {
|
||||
return chain([a, b, c, d, e, f]).map((r) {
|
||||
return Tuple6(r.value[0] as A, r.value[1] as B, r.value[2] as C,
|
||||
r.value[3] as D, r.value[4] as E, r.value[5] as F);
|
||||
});
|
||||
}
|
||||
|
||||
/// A typed parser that parses a sequence of 7 values of different types.
|
||||
Parser<Tuple7<A, B, C, D, E, F, G>> tuple7<A, B, C, D, E, F, G>(
|
||||
Parser<A> a,
|
||||
Parser<B> b,
|
||||
Parser<C> c,
|
||||
Parser<D> d,
|
||||
Parser<E> e,
|
||||
Parser<F> f,
|
||||
Parser<G> g) {
|
||||
return chain([a, b, c, d, e, f, g]).map((r) {
|
||||
return Tuple7(r.value[0] as A, r.value[1] as B, r.value[2] as C,
|
||||
r.value[3] as D, r.value[4] as E, r.value[5] as F, r.value[6] as G);
|
||||
});
|
||||
}
|
25
packages/combinator/lib/src/combinator/value.dart
Normal file
25
packages/combinator/lib/src/combinator/value.dart
Normal file
|
@ -0,0 +1,25 @@
|
|||
part of lex.src.combinator;
|
||||
|
||||
class _Value<T> extends Parser<T> {
|
||||
final Parser<T> parser;
|
||||
final T Function(ParseResult<T>) f;
|
||||
|
||||
_Value(this.parser, this.f);
|
||||
|
||||
@override
|
||||
ParseResult<T> __parse(ParseArgs args) {
|
||||
var result = parser._parse(args.increaseDepth()).change(parser: this);
|
||||
return result.successful ? result.change(value: f(result)) : result;
|
||||
}
|
||||
|
||||
@override
|
||||
void stringify(CodeBuffer buffer) {
|
||||
buffer
|
||||
..writeln('set value($f) (')
|
||||
..indent();
|
||||
parser.stringify(buffer);
|
||||
buffer
|
||||
..outdent()
|
||||
..writeln(')');
|
||||
}
|
||||
}
|
23
packages/combinator/lib/src/error.dart
Normal file
23
packages/combinator/lib/src/error.dart
Normal file
|
@ -0,0 +1,23 @@
|
|||
import 'package:source_span/source_span.dart';
|
||||
|
||||
class SyntaxError implements Exception {
|
||||
final SyntaxErrorSeverity severity;
|
||||
final String message;
|
||||
final FileSpan span;
|
||||
String _toolString;
|
||||
|
||||
SyntaxError(this.severity, this.message, this.span);
|
||||
|
||||
String get toolString {
|
||||
if (_toolString != null) return _toolString;
|
||||
var type = severity == SyntaxErrorSeverity.warning ? 'warning' : 'error';
|
||||
return _toolString = '$type: ${span.start.toolString}: $message';
|
||||
}
|
||||
}
|
||||
|
||||
enum SyntaxErrorSeverity {
|
||||
warning,
|
||||
error,
|
||||
info,
|
||||
hint,
|
||||
}
|
20
packages/combinator/pubspec.yaml
Normal file
20
packages/combinator/pubspec.yaml
Normal file
|
@ -0,0 +1,20 @@
|
|||
name: combinator
|
||||
version: 2.0.0
|
||||
description: Packrat parser combinators that support static typing, generics, file spans, memoization, and more.
|
||||
author: Tobe O <thosakwe@gmail.com>
|
||||
homepage: https://github.com/thosakwe/combinator.git
|
||||
publish_to: none
|
||||
environment:
|
||||
sdk: ">=2.10.0 <3.0.0"
|
||||
dependencies:
|
||||
code_buffer:
|
||||
git:
|
||||
url: https://github.com/dukefirehawk/angel.git
|
||||
ref: sdk-2.12.x_nnbd
|
||||
path: packages/code_buffer
|
||||
matcher: ^0.12.0
|
||||
source_span: ^1.8.1
|
||||
string_scanner: ^1.1.0
|
||||
tuple: ^2.0.0
|
||||
dev_dependencies:
|
||||
test: ^1.16.8
|
12
packages/combinator/test/all.dart
Normal file
12
packages/combinator/test/all.dart
Normal file
|
@ -0,0 +1,12 @@
|
|||
import 'package:test/test.dart';
|
||||
import 'list_test.dart' as list;
|
||||
import 'match_test.dart' as match;
|
||||
import 'misc_test.dart' as misc;
|
||||
import 'value_test.dart' as value;
|
||||
|
||||
main() {
|
||||
group('list', list.main);
|
||||
group('match', match.main);
|
||||
group('value', value.main);
|
||||
misc.main();
|
||||
}
|
3
packages/combinator/test/common.dart
Normal file
3
packages/combinator/test/common.dart
Normal file
|
@ -0,0 +1,3 @@
|
|||
import 'package:string_scanner/string_scanner.dart';
|
||||
|
||||
SpanScanner scan(String text) => new SpanScanner(text);
|
22
packages/combinator/test/list_test.dart
Normal file
22
packages/combinator/test/list_test.dart
Normal file
|
@ -0,0 +1,22 @@
|
|||
import 'package:combinator/combinator.dart';
|
||||
import 'package:test/test.dart';
|
||||
import 'common.dart';
|
||||
|
||||
main() {
|
||||
var number = chain([
|
||||
match(new RegExp(r'[0-9]+')).value((r) => int.parse(r.span.text)),
|
||||
match(',').opt(),
|
||||
]).first().cast<int>();
|
||||
|
||||
var numbers = number.plus();
|
||||
|
||||
test('sort', () {
|
||||
var parser = numbers.sort((a, b) => a.compareTo(b));
|
||||
expect(parser.parse(scan('21,2,3,34,20')).value, [2, 3, 20, 21, 34]);
|
||||
});
|
||||
test('reduce', () {
|
||||
var parser = numbers.reduce((a, b) => a + b);
|
||||
expect(parser.parse(scan('21,2,3,34,20')).value, 80);
|
||||
expect(parser.parse(scan('not numbers')).value, isNull);
|
||||
});
|
||||
}
|
16
packages/combinator/test/match_test.dart
Normal file
16
packages/combinator/test/match_test.dart
Normal file
|
@ -0,0 +1,16 @@
|
|||
import 'package:combinator/combinator.dart';
|
||||
import 'package:test/test.dart';
|
||||
import 'common.dart';
|
||||
|
||||
main() {
|
||||
test('match string', () {
|
||||
expect(match('hello').parse(scan('hello world')).successful, isTrue);
|
||||
});
|
||||
test('match start only', () {
|
||||
expect(match('hello').parse(scan('goodbye hello')).successful, isFalse);
|
||||
});
|
||||
|
||||
test('fail if no match', () {
|
||||
expect(match('hello').parse(scan('world')).successful, isFalse);
|
||||
});
|
||||
}
|
66
packages/combinator/test/misc_test.dart
Normal file
66
packages/combinator/test/misc_test.dart
Normal file
|
@ -0,0 +1,66 @@
|
|||
import 'package:combinator/combinator.dart';
|
||||
import 'package:matcher/matcher.dart';
|
||||
import 'package:test/test.dart';
|
||||
import 'common.dart';
|
||||
|
||||
main() {
|
||||
test('advance', () {
|
||||
var scanner = scan('hello world');
|
||||
|
||||
// Casted -> dynamic just for the sake of coverage.
|
||||
var parser = match('he').forward(2).castDynamic();
|
||||
parser.parse(scanner);
|
||||
expect(scanner.position, 4);
|
||||
});
|
||||
|
||||
test('change', () {
|
||||
var parser = match('hello').change((r) => r.change(value: 23));
|
||||
expect(parser.parse(scan('helloworld')).value, 23);
|
||||
});
|
||||
|
||||
test('check', () {
|
||||
var parser = match<int>(new RegExp(r'[A-Za-z]+'))
|
||||
.value((r) => r.span.length)
|
||||
.check(greaterThan(3));
|
||||
expect(parser.parse(scan('helloworld')).successful, isTrue);
|
||||
expect(parser.parse(scan('yo')).successful, isFalse);
|
||||
});
|
||||
|
||||
test('map', () {
|
||||
var parser = match(new RegExp(r'[A-Za-z]+')).map<int>((r) => r.span.length);
|
||||
expect(parser.parse(scan('hello')).value, 5);
|
||||
});
|
||||
|
||||
test('negate', () {
|
||||
var parser = match('hello').negate(errorMessage: 'world');
|
||||
expect(parser.parse(scan('goodbye world')).successful, isTrue);
|
||||
expect(parser.parse(scan('hello world')).successful, isFalse);
|
||||
expect(parser.parse(scan('hello world')).errors.first.message, 'world');
|
||||
});
|
||||
|
||||
group('opt', () {
|
||||
var single = match('hello').opt(backtrack: true);
|
||||
var list = match('hel').then(match('lo')).opt();
|
||||
|
||||
test('succeeds if present', () {
|
||||
expect(single.parse(scan('hello')).successful, isTrue);
|
||||
expect(list.parse(scan('hello')).successful, isTrue);
|
||||
});
|
||||
|
||||
test('succeeds if not present', () {
|
||||
expect(single.parse(scan('goodbye')).successful, isTrue);
|
||||
expect(list.parse(scan('goodbye')).successful, isTrue);
|
||||
});
|
||||
|
||||
test('backtracks if not present', () {
|
||||
for (var parser in [single, list]) {
|
||||
var scanner = scan('goodbye');
|
||||
var pos = scanner.position;
|
||||
parser.parse(scanner);
|
||||
expect(scanner.position, pos);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
test('safe', () {});
|
||||
}
|
57
packages/combinator/test/recursion_test.dart
Normal file
57
packages/combinator/test/recursion_test.dart
Normal file
|
@ -0,0 +1,57 @@
|
|||
import 'package:combinator/combinator.dart';
|
||||
import 'package:string_scanner/string_scanner.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
void main() {}
|
||||
|
||||
/*
|
||||
void main() {
|
||||
var number = match(new RegExp(r'-?[0-9]+(\.[0-9]+)?'))
|
||||
.map<num>((r) => num.parse(r.span.text));
|
||||
|
||||
var term = reference<num>();
|
||||
|
||||
var r = new Recursion<num>();
|
||||
|
||||
r.prefix = [number];
|
||||
|
||||
r.infix.addAll({
|
||||
match('*'): (l, r, _) => l * r,
|
||||
match('/'): (l, r, _) => l / r,
|
||||
match('+'): (l, r, _) => l + r,
|
||||
match('-'): (l, r, _) => l - r,
|
||||
|
||||
|
||||
match('-'): (l, r, _) => l - r,
|
||||
match('+'): (l, r, _) => l + r,
|
||||
match('/'): (l, r, _) => l / r,
|
||||
match('*'): (l, r, _) => l * r,
|
||||
|
||||
});
|
||||
|
||||
term.parser = r.precedence(0);
|
||||
|
||||
num parse(String text) {
|
||||
var scanner = new SpanScanner(text);
|
||||
var result = term.parse(scanner);
|
||||
print(result.span.highlight());
|
||||
return result.value;
|
||||
}
|
||||
|
||||
test('prefix', () {
|
||||
expect(parse('24'), 24);
|
||||
});
|
||||
|
||||
test('infix', () {
|
||||
expect(parse('12/6'), 2);
|
||||
expect(parse('24+23'), 47);
|
||||
expect(parse('24-23'), 1);
|
||||
expect(parse('4*3'), 12);
|
||||
});
|
||||
|
||||
test('precedence', () {
|
||||
expect(parse('2+3*5*2'), 15);
|
||||
//expect(parse('2+3+5-2*2'), 15);
|
||||
});
|
||||
}
|
||||
*/
|
15
packages/combinator/test/value_test.dart
Normal file
15
packages/combinator/test/value_test.dart
Normal file
|
@ -0,0 +1,15 @@
|
|||
import 'package:combinator/combinator.dart';
|
||||
import 'package:test/test.dart';
|
||||
import 'common.dart';
|
||||
|
||||
main() {
|
||||
var parser = match('hello').value((r) => 'world');
|
||||
|
||||
test('sets value', () {
|
||||
expect(parser.parse(scan('hello world')).value, 'world');
|
||||
});
|
||||
|
||||
test('no value if no match', () {
|
||||
expect(parser.parse(scan('goodbye world')).value, isNull);
|
||||
});
|
||||
}
|
Loading…
Reference in a new issue