-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
230 additions
and
203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
## [Unreleased] | ||
### Added | ||
- Tokenizer and AST | ||
|
||
## 0.0.0+dev.0 - 2020-07-24 | ||
### Added | ||
- Basic design draft | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,4 @@ | ||
/// JSONPath for Dart | ||
library jessie; | ||
|
||
export 'package:jessie/src/field.dart'; | ||
export 'package:jessie/src/filter.dart'; | ||
export 'package:jessie/src/index.dart'; | ||
export 'package:jessie/src/neutral.dart'; | ||
export 'package:jessie/src/tokenizer.dart'; | ||
export 'package:jessie/src/json_path.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
class Node { | ||
Node(this.value); | ||
|
||
static Node build(List<String> tokens) { | ||
final root = Node(r'$'); | ||
if (tokens.isEmpty) { | ||
return root; | ||
} | ||
final reversed = [...tokens.reversed]; | ||
if (reversed.last == r'$') { | ||
reversed.removeLast(); | ||
} | ||
final stack = <Node>[root]; | ||
while (reversed.isNotEmpty) { | ||
final token = reversed.removeLast(); | ||
if (token == '[') { | ||
stack.add(Node(token)); | ||
continue; | ||
} | ||
if (token == ']' || token == ')') { | ||
final search = token == ']' ? '[' : '('; | ||
final children = <Node>[]; | ||
while (stack.last.value != search) { | ||
children.add(stack.removeLast()); | ||
} | ||
final brackets = stack.removeLast(); | ||
brackets.children.addAll(children); | ||
stack.last.children.add(brackets); | ||
continue; | ||
} | ||
stack.last.children.add(Node(token)); | ||
} | ||
return stack.last; | ||
} | ||
|
||
final String value; | ||
final children = <Node>[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import 'package:jessie/src/ast.dart'; | ||
import 'package:jessie/src/filter.dart'; | ||
import 'package:jessie/src/match.dart'; | ||
import 'package:jessie/src/root.dart'; | ||
import 'package:jessie/src/state.dart'; | ||
import 'package:jessie/src/tokenize.dart'; | ||
|
||
class JsonPath { | ||
factory JsonPath(String expression) { | ||
State state = Ready(Root()); | ||
for (final node in Node.build(tokenize(expression)).children) { | ||
state = state.process(node); | ||
} | ||
return JsonPath._(state.filter); | ||
} | ||
|
||
JsonPath._(this._filter); | ||
|
||
final Filter _filter; | ||
|
||
/// Filters the given [json]. | ||
/// Returns an Iterable of all elements found | ||
Iterable<PathMatch> filter(json) => _filter.call([PathMatch(json, '')]); | ||
|
||
@override | ||
String toString() => _filter.toString(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class PathMatch<T> { | ||
PathMatch(this.value, this.path); | ||
|
||
final T value; | ||
|
||
final String path; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import 'package:jessie/src/filter.dart'; | ||
import 'package:jessie/src/match.dart'; | ||
|
||
class Root extends Filter { | ||
const Root(); | ||
|
||
@override | ||
Iterable<PathMatch> call(Iterable<PathMatch> matches) => | ||
matches.map((m) => PathMatch(m.value, toString())); | ||
|
||
@override | ||
String toString() => r'$'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import 'package:jessie/src/ast.dart'; | ||
import 'package:jessie/src/field.dart'; | ||
import 'package:jessie/src/filter.dart'; | ||
import 'package:jessie/src/index.dart'; | ||
|
||
abstract class State { | ||
State process(Node node); | ||
|
||
Filter get filter; | ||
} | ||
|
||
class Ready implements State { | ||
Ready(this.filter); | ||
|
||
@override | ||
final Filter filter; | ||
|
||
@override | ||
State process(Node node) { | ||
if (node.value == '[') { | ||
return Ready(filter.then(Index(int.parse(node.children.first.value)))); | ||
} | ||
if (node.value == '.') { | ||
return AwaitingField(filter); | ||
} | ||
throw StateError('Got ${node.value} in $this'); | ||
} | ||
} | ||
|
||
class AwaitingField implements State { | ||
AwaitingField(this.filter); | ||
|
||
@override | ||
final Filter filter; | ||
|
||
@override | ||
State process(Node node) { | ||
return Ready(filter.then(Field(node.value))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
List<String> tokenize(String expr) { | ||
final tokens = <String>[]; | ||
var pos = 0; | ||
while (pos < expr.length) { | ||
var token = ''; | ||
while (pos < expr.length) { | ||
if (_singles.contains(expr[pos])) { | ||
if (token.isNotEmpty) { | ||
break; | ||
} | ||
if (expr.length > pos + 1) { | ||
final nextTwoChars = expr.substring(pos, pos + 1); | ||
if (_doubles.contains(nextTwoChars)) { | ||
token = nextTwoChars; | ||
pos += 2; | ||
break; | ||
} | ||
} | ||
token = expr[pos++]; | ||
break; | ||
} | ||
token += expr[pos++]; | ||
} | ||
tokens.add(token); | ||
} | ||
return tokens; | ||
} | ||
|
||
const _singles = [ | ||
r'$', | ||
'[', | ||
']', | ||
'(', | ||
')', | ||
'?', | ||
'.', | ||
'*', | ||
'@', | ||
'<', | ||
'>', | ||
'-', | ||
'=', | ||
'&', | ||
'|', | ||
':', | ||
' ' | ||
]; | ||
|
||
const _doubles = ['..', '&&', '||']; |
Oops, something went wrong.