From e2da6a8ee58ab8ec2fe464477b9a44f8b0b5dca4 Mon Sep 17 00:00:00 2001 From: Nik Everett Date: Thu, 19 Jan 2017 11:04:08 -0500 Subject: [PATCH 1/4] Improve painless's javadocs Hopefully useful references. --- .../painless/antlr/package-info.java | 24 +++++++++++++++++++ .../elasticsearch/painless/package-info.java | 23 ++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 modules/lang-painless/src/main/java/org/elasticsearch/painless/antlr/package-info.java create mode 100644 modules/lang-painless/src/main/java/org/elasticsearch/painless/package-info.java diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/antlr/package-info.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/antlr/package-info.java new file mode 100644 index 00000000000..041118ba029 --- /dev/null +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/antlr/package-info.java @@ -0,0 +1,24 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + * Lexer, parser, and tree {@link Walker} responsible for turning the code + * generating nodes in {@link org.elasticsearch.painless.node}. + */ +package org.elasticsearch.painless.antlr; diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/package-info.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/package-info.java new file mode 100644 index 00000000000..d45d4fb6d49 --- /dev/null +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/package-info.java @@ -0,0 +1,23 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + * Implementation of the Painless language. + */ +package org.elasticsearch.painless; From dbb4a2ca6c2376985b43544c92f37bc102840b27 Mon Sep 17 00:00:00 2001 From: Nik Everett Date: Thu, 19 Jan 2017 11:23:16 -0500 Subject: [PATCH 2/4] Move lexer hacks to EnhancedPainlessLexer This "feels" nicer. Less classes at least. --- .../src/main/antlr/PainlessLexer.g4 | 16 ++++-- .../painless/antlr/EnhancedPainlessLexer.java | 33 ++++++++++++- .../painless/antlr/PainlessLexer.java | 15 ++++-- .../painless/antlr/SlashStrategy.java | 49 ------------------- 4 files changed, 54 insertions(+), 59 deletions(-) delete mode 100644 modules/lang-painless/src/main/java/org/elasticsearch/painless/antlr/SlashStrategy.java diff --git a/modules/lang-painless/src/main/antlr/PainlessLexer.g4 b/modules/lang-painless/src/main/antlr/PainlessLexer.g4 index 18fdae751af..2d5af7c483a 100644 --- a/modules/lang-painless/src/main/antlr/PainlessLexer.g4 +++ b/modules/lang-painless/src/main/antlr/PainlessLexer.g4 @@ -20,7 +20,15 @@ lexer grammar PainlessLexer; @header { -import org.elasticsearch.painless.Definition; +} + +@members{ + protected boolean isSimpleType(String name) { + throw new UnsupportedOperationException("Must be implemented in a subclass"); + } + protected boolean slashIsRegex() { + throw new UnsupportedOperationException("Must be implemented in a subclass"); + } } WS: [ \t\n\r]+ -> skip; @@ -59,7 +67,7 @@ INSTANCEOF: 'instanceof'; BOOLNOT: '!'; BWNOT: '~'; MUL: '*'; -DIV: '/' { false == SlashStrategy.slashIsRegex(this) }?; +DIV: '/' { false == slashIsRegex() }?; REM: '%'; ADD: '+'; SUB: '-'; @@ -108,7 +116,7 @@ INTEGER: ( '0' | [1-9] [0-9]* ) [lLfFdD]?; DECIMAL: ( '0' | [1-9] [0-9]* ) (DOT [0-9]+)? ( [eE] [+\-]? [0-9]+ )? [fFdD]?; STRING: ( '"' ( '\\"' | '\\\\' | ~[\\"] )*? '"' ) | ( '\'' ( '\\\'' | '\\\\' | ~[\\'] )*? '\'' ); -REGEX: '/' ( ~('/' | '\n') | '\\' ~'\n' )+ '/' [cilmsUux]* { SlashStrategy.slashIsRegex(this) }?; +REGEX: '/' ( ~('/' | '\n') | '\\' ~'\n' )+ '/' [cilmsUux]* { slashIsRegex() }?; TRUE: 'true'; FALSE: 'false'; @@ -121,7 +129,7 @@ NULL: 'null'; // or not. Note this works by processing one character at a time // and the rule is added or removed as this happens. This is also known // as "the lexer hack." See (https://en.wikipedia.org/wiki/The_lexer_hack). -TYPE: ID ( DOT ID )* { Definition.isSimpleType(getText()) }?; +TYPE: ID ( DOT ID )* { isSimpleType(getText()) }?; ID: [_a-zA-Z] [_a-zA-Z0-9]*; mode AFTER_DOT; diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/antlr/EnhancedPainlessLexer.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/antlr/EnhancedPainlessLexer.java index 244c2f38e62..640d9c29b20 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/antlr/EnhancedPainlessLexer.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/antlr/EnhancedPainlessLexer.java @@ -26,13 +26,15 @@ import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.TokenSource; import org.antlr.v4.runtime.misc.Interval; import org.antlr.v4.runtime.misc.Pair; +import org.elasticsearch.painless.Definition; import org.elasticsearch.painless.Location; /** * A lexer that is customized for painless. It: *