-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstrings.dart
86 lines (66 loc) · 2.38 KB
/
strings.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import 'package:json_path/src/grammar/child_selector.dart';
import 'package:json_path/src/grammar/parser_ext.dart';
import 'package:petitparser/petitparser.dart';
final _escape = char(r'\');
final _doubleQuote = char('"');
final _singleQuote = char("'");
final _escapedSlash = string(r'\/').value(r'/');
final _escapedBackSlash = string(r'\\').value(r'\');
final _escapedBackspace = string(r'\b').value('\b');
final _escapedFormFeed = string(r'\f').value('\f');
final _escapedNewLine = string(r'\n').value('\n');
final _escapedReturn = string(r'\r').value('\r');
final _escapedTab = string(r'\t').value('\t');
final _escapedControl = [
_escapedSlash,
_escapedBackSlash,
_escapedBackspace,
_escapedFormFeed,
_escapedNewLine,
_escapedReturn,
_escapedTab
].toChoiceParser();
// The highest unicode character
final _unicodeBoundary = String.fromCharCode(0xFFFF);
// Exclude double quote '"' and back slash '\'
final _doubleUnescaped = [
range(' ', '!'),
range('#', '['),
range(']', _unicodeBoundary)
].toChoiceParser();
final _hexDigit = anyOf('0123456789ABCDEFabcdef');
final _unicodeSymbol = (string(r'\u') & _hexDigit.timesString(4))
.map((value) => String.fromCharCode(int.parse(value.last, radix: 16)));
final _escapedDoubleQuote = (_escape & _doubleQuote).map((_) => '"');
final _doubleInner = [
_doubleUnescaped,
_escapedDoubleQuote,
_escapedControl,
_unicodeSymbol
].toChoiceParser().star().join();
// Exclude single quote "'" and back slash "\"
final _singleUnescaped = [
range(' ', '&'),
range('(', '['),
range(']', _unicodeBoundary)
].toChoiceParser();
final _escapedSingleQuote = (_escape & _singleQuote).map((_) => "'");
final _singleInner = [
_singleUnescaped,
_escapedSingleQuote,
_escapedControl,
_unicodeSymbol
].toChoiceParser().star().join();
final _doubleQuotedString =
_doubleInner.skip(before: _doubleQuote, after: _doubleQuote);
final _singleQuotedString =
_singleInner.skip(before: _singleQuote, after: _singleQuote);
final _nameFirst =
(char('_') | letter() | range(String.fromCharCode(0x80), _unicodeBoundary))
.plus()
.flatten('a correct member name expected');
final _nameChar = digit() | _nameFirst;
final quotedString = (_singleQuotedString | _doubleQuotedString).cast<String>();
final memberNameShorthand = (_nameFirst & _nameChar.star())
.flatten('a member name shorthand expected')
.map(childSelector);