-
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
7 changed files
with
92 additions
and
38 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
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,12 @@ | ||
/// A single-quoted string. | ||
/// Example: | ||
/// `hello` => `'hello'` | ||
/// `i'm ok` => `'i\'m ok'` | ||
class Quote { | ||
Quote(String value) : value = "'${value.replaceAll("'", r"\'")}'"; | ||
|
||
final String value; | ||
|
||
@override | ||
String toString() => 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
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,25 @@ | ||
import 'package:jessie/src/quote.dart'; | ||
import 'package:jessie/src/result.dart'; | ||
import 'package:jessie/src/selector/selector.dart'; | ||
|
||
class Recursive extends Selector { | ||
@override | ||
Iterable<Result> call(Iterable<Result> results) => results.map((r) { | ||
final val = r.value; | ||
final self = [r]; | ||
if (val is Map) { | ||
return self.followedBy(call(val.entries | ||
.map((e) => Result(e.value, r.path + '[${Quote(e.key)}]')))); | ||
} | ||
if (val is List) { | ||
return self.followedBy(call(val | ||
.asMap() | ||
.entries | ||
.map((e) => Result(e.value, r.path + '[${e.key}]')))); | ||
} | ||
return <Result>[]; | ||
}).expand((_) => _); | ||
|
||
@override | ||
String get expression => '..'; | ||
} |
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,51 +1,54 @@ | ||
import 'package:jessie/src/ast.dart'; | ||
import 'package:jessie/src/selector/all_in_array.dart'; | ||
import 'package:jessie/src/selector/field.dart'; | ||
import 'package:jessie/src/selector/selector.dart'; | ||
import 'package:jessie/src/selector/index.dart'; | ||
import 'package:jessie/src/selector/recursive.dart'; | ||
import 'package:jessie/src/selector/selector.dart'; | ||
|
||
abstract class State { | ||
State process(Node node); | ||
|
||
Selector get filter; | ||
Selector get selector; | ||
} | ||
|
||
class Ready implements State { | ||
Ready(this.filter); | ||
Ready(this.selector); | ||
|
||
@override | ||
final Selector filter; | ||
final Selector selector; | ||
|
||
@override | ||
State process(Node node) { | ||
if (node.value == '[') { | ||
return Ready(filter.then(_brackets(node.children))); | ||
return Ready(selector.then(_brackets(node.children))); | ||
} | ||
if (node.value == '.') { | ||
return AwaitingField(filter); | ||
return AwaitingField(selector); | ||
} | ||
if (node.value == '..') { | ||
return Ready(selector.then(Recursive())); | ||
} | ||
throw StateError('Got ${node.value} in $this'); | ||
} | ||
|
||
Selector _brackets(List<Node> nodes) { | ||
if (nodes.length == 1) { | ||
final node = nodes.single; | ||
final val = node.value; | ||
if (val == '*') return AllInArray(); | ||
if (node.value == '*') return AllInArray(); | ||
if (node.isNumber) return Index(int.parse(nodes.first.value)); | ||
} | ||
throw StateError('Unexpected bracket expression'); | ||
} | ||
} | ||
|
||
class AwaitingField implements State { | ||
AwaitingField(this.filter); | ||
AwaitingField(this.selector); | ||
|
||
@override | ||
final Selector filter; | ||
final Selector selector; | ||
|
||
@override | ||
State process(Node node) { | ||
return Ready(filter.then(Field(node.value))); | ||
return Ready(selector.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
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