mirror of
https://github.com/aNNiMON/Own-Programming-Language-Tutorial.git
synced 2024-09-20 08:44:20 +03:00
Синтаксический сахар - доступ к элементам map через точку
This commit is contained in:
parent
d0c8c75734
commit
794d6f2d76
@ -19,42 +19,42 @@ thread(::http, "https://api.github.com/events", def(r) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
def show_github_events(event) {
|
def show_github_events(event) {
|
||||||
println event["created_at"]
|
println event.created_at
|
||||||
actor = event["actor"]
|
actor = event.actor
|
||||||
println "User: https://github.com/" + actor["login"]
|
println "User: https://github.com/" + actor.login
|
||||||
println github_event_type(event)
|
println github_event_type(event)
|
||||||
println "-" * 50
|
println "-" * 50
|
||||||
}
|
}
|
||||||
|
|
||||||
def github_event_type(event) {
|
def github_event_type(event) {
|
||||||
type = event["type"]
|
type = event.type
|
||||||
repo = "https://github.com/" + event["repo"]["name"]
|
repo = "https://github.com/" + event.repo.name
|
||||||
payload = event["payload"]
|
payload = event.payload
|
||||||
|
|
||||||
if (type == "CommitCommentEvent") {
|
if (type == "CommitCommentEvent") {
|
||||||
return "commented commit in " + repo + "\n" + payload["comment"]["body"]
|
return "commented commit in " + repo + "\n" + payload.comment.body
|
||||||
}
|
}
|
||||||
if (type == "CreateEvent") {
|
if (type == "CreateEvent") {
|
||||||
return "created " + payload["ref_type"] + " on " + repo
|
return "created " + payload.ref_type + " on " + repo
|
||||||
}
|
}
|
||||||
if (type == "DeleteEvent") {
|
if (type == "DeleteEvent") {
|
||||||
return "deleted " + payload["ref_type"] + " on " + repo
|
return "deleted " + payload.ref_type + " on " + repo
|
||||||
}
|
}
|
||||||
if (type == "ForkEvent") {
|
if (type == "ForkEvent") {
|
||||||
return "forked repository " + repo
|
return "forked repository " + repo
|
||||||
}
|
}
|
||||||
if (type == "IssueCommentEvent") {
|
if (type == "IssueCommentEvent") {
|
||||||
return "commented issue " + payload["issue"]["title"] + " on " + repo + "\n" + payload["comment"]["body"]
|
return "commented issue " + payload.issue.title + " on " + repo + "\n" + payload.comment.body
|
||||||
}
|
}
|
||||||
if (type == "IssuesEvent") {
|
if (type == "IssuesEvent") {
|
||||||
return payload["action"] + " issue '" + payload["issue"]["title"] + "' on " + repo
|
return payload.action + " issue '" + payload.issue.title + "' on " + repo
|
||||||
}
|
}
|
||||||
if (type == "PullRequestEvent") {
|
if (type == "PullRequestEvent") {
|
||||||
pr = payload["pull_request"]
|
pr = payload.pull_request
|
||||||
return payload["action"] + " pull request #" + payload["number"] + " '" + pr["title"] + "' on " + repo
|
return payload.action + " pull request #" + payload.number + " '" + pr.title + "' on " + repo
|
||||||
}
|
}
|
||||||
if (type == "PushEvent") {
|
if (type == "PushEvent") {
|
||||||
return "pushed " + length(payload["commits"]) + " commits to " + repo
|
return "pushed " + length(payload.commits) + " commits to " + repo
|
||||||
}
|
}
|
||||||
if (type == "WatchEvent") {
|
if (type == "WatchEvent") {
|
||||||
return "start watching repository " + repo
|
return "start watching repository " + repo
|
||||||
|
@ -11,7 +11,7 @@ import java.util.Map;
|
|||||||
*/
|
*/
|
||||||
public final class Lexer {
|
public final class Lexer {
|
||||||
|
|
||||||
private static final String OPERATOR_CHARS = "+-*/%()[]{}=<>!&|,^~?:";
|
private static final String OPERATOR_CHARS = "+-*/%()[]{}=<>!&|.,^~?:";
|
||||||
|
|
||||||
private static final Map<String, TokenType> OPERATORS;
|
private static final Map<String, TokenType> OPERATORS;
|
||||||
static {
|
static {
|
||||||
@ -30,6 +30,7 @@ public final class Lexer {
|
|||||||
OPERATORS.put("=", TokenType.EQ);
|
OPERATORS.put("=", TokenType.EQ);
|
||||||
OPERATORS.put("<", TokenType.LT);
|
OPERATORS.put("<", TokenType.LT);
|
||||||
OPERATORS.put(">", TokenType.GT);
|
OPERATORS.put(">", TokenType.GT);
|
||||||
|
OPERATORS.put(".", TokenType.DOT);
|
||||||
OPERATORS.put(",", TokenType.COMMA);
|
OPERATORS.put(",", TokenType.COMMA);
|
||||||
OPERATORS.put("^", TokenType.CARET);
|
OPERATORS.put("^", TokenType.CARET);
|
||||||
OPERATORS.put("~", TokenType.TILDE);
|
OPERATORS.put("~", TokenType.TILDE);
|
||||||
|
@ -237,6 +237,19 @@ public final class Parser {
|
|||||||
return new ArrayAccessExpression(variable, indices);
|
return new ArrayAccessExpression(variable, indices);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private ArrayAccessExpression object() {
|
||||||
|
// object.field1.field2
|
||||||
|
// Syntaxic sugar for object["field1"]["field2"]
|
||||||
|
final String variable = consume(TokenType.WORD).getText();
|
||||||
|
final List<Expression> indices = new ArrayList<>();
|
||||||
|
while (match(TokenType.DOT)) {
|
||||||
|
final String fieldName = consume(TokenType.WORD).getText();
|
||||||
|
final Expression key = new ValueExpression(fieldName);
|
||||||
|
indices.add(key);
|
||||||
|
}
|
||||||
|
return new ArrayAccessExpression(variable, indices);
|
||||||
|
}
|
||||||
|
|
||||||
private Expression expression() {
|
private Expression expression() {
|
||||||
return ternary();
|
return ternary();
|
||||||
}
|
}
|
||||||
@ -459,6 +472,9 @@ public final class Parser {
|
|||||||
if (lookMatch(0, TokenType.WORD) && lookMatch(1, TokenType.LPAREN)) {
|
if (lookMatch(0, TokenType.WORD) && lookMatch(1, TokenType.LPAREN)) {
|
||||||
return function();
|
return function();
|
||||||
}
|
}
|
||||||
|
if (lookMatch(0, TokenType.WORD) && lookMatch(1, TokenType.DOT)) {
|
||||||
|
return object();
|
||||||
|
}
|
||||||
if (lookMatch(0, TokenType.LBRACKET)) {
|
if (lookMatch(0, TokenType.LBRACKET)) {
|
||||||
return array();
|
return array();
|
||||||
}
|
}
|
||||||
|
@ -61,6 +61,7 @@ public enum TokenType {
|
|||||||
LBRACE, // {
|
LBRACE, // {
|
||||||
RBRACE, // }
|
RBRACE, // }
|
||||||
COMMA, // ,
|
COMMA, // ,
|
||||||
|
DOT, // .
|
||||||
|
|
||||||
EOF
|
EOF
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user