From c250a4dcbf0c876e5fdc7b6f961be34fb30b7aaa Mon Sep 17 00:00:00 2001 From: Daniel Kamkha Date: Tue, 8 Dec 2020 15:03:17 +0100 Subject: [PATCH] Add LIME IDL support for "validator" keyword Updated LIME IDL grammar and model builder to add support for `validator` keyword in classes. See: #637 Signed-off-by: Daniel Kamkha --- docs/lime_idl.md | 14 ++++++++ examples/libhello/lime/test/Validator.lime | 33 ++++++++++++++++++ .../templates/lime/LimeClass.mustache | 1 + .../java/com/here/gluecodium/SmokeTest.kt | 1 - .../smoke/validator/input/Validator.lime | 29 +++++++++++++++ .../output/lime/smoke/ValidatedClass.lime | 9 +++++ lime-loader/src/main/antlr/LimeLexer.g4 | 1 + lime-loader/src/main/antlr/LimeParser.g4 | 6 +++- .../loader/AntlrLimeModelBuilder.kt | 3 +- .../loader/AntlrLimeModelBuilderBase.kt | 4 +++ .../here/gluecodium/model/lime/LimeClass.kt | 5 ++- tools/Lime_IDL_file_type_for_IntelliJ.jar | Bin 1083 -> 1070 bytes 12 files changed, 102 insertions(+), 4 deletions(-) create mode 100644 examples/libhello/lime/test/Validator.lime create mode 100644 gluecodium/src/test/resources/smoke/validator/input/Validator.lime create mode 100644 gluecodium/src/test/resources/smoke/validator/output/lime/smoke/ValidatedClass.lime diff --git a/docs/lime_idl.md b/docs/lime_idl.md index d2633d81f2..936b25a7ab 100644 --- a/docs/lime_idl.md +++ b/docs/lime_idl.md @@ -353,6 +353,20 @@ attributes (or omitted if the name is the name of the type in IDL declaration). type which is generated when a converter is specified has an additional `_internal` suffix to its name in Swift and Dart. +#### Validator + +* Syntax: **validator** *ValidatorName* **()** +* Example: `validator checkState()` +* Can be placed in: class. If present, it must be: + * The first child element, if no external descriptor is present. + * The second child element, if an external descriptor is present. +* Description: enables state validation and specifies the name for the validator function. For C++ a pure virtual const +function with *ValidatorName* name is generated, that takes no parameters and returns an `optional`. For +Java/Swift/Dart a private function with *ValidatorName* name is generated, that takes no parameters and returns no +value. This private function is called before each other non-static function (method or property accessor) call is +executed. If the C++ function returns a non-empty optional, the Java/Swift/Dart function throws an unchecked exception +with the returned string value as a message. If the returned optional value is empty, no exception is thrown. + ### Type references A type reference is a mention of a type anywhere, as opposed to a type declaration. A type reference diff --git a/examples/libhello/lime/test/Validator.lime b/examples/libhello/lime/test/Validator.lime new file mode 100644 index 0000000000..a849247632 --- /dev/null +++ b/examples/libhello/lime/test/Validator.lime @@ -0,0 +1,33 @@ +# Copyright (C) 2016-2020 HERE Europe B.V. +# +# Licensed 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. +# +# SPDX-License-Identifier: Apache-2.0 +# License-Filename: LICENSE + +package test + +class ValidatedClass { + validator checkMe() + constructor makeMe() + + fun doStuff() + static fun doStaticStuff() + + property something: String + static property somethingStatic: String +} + +class ValidationController { + static fun setFailState(state: Boolean) +} diff --git a/gluecodium/src/main/resources/templates/lime/LimeClass.mustache b/gluecodium/src/main/resources/templates/lime/LimeClass.mustache index 121ef4248e..da5abf3552 100644 --- a/gluecodium/src/main/resources/templates/lime/LimeClass.mustache +++ b/gluecodium/src/main/resources/templates/lime/LimeClass.mustache @@ -21,5 +21,6 @@ {{>lime/LimeDocumentation}} {{visibility}}class {{escapedName}} {{#parent}}: {{type.name}} {{/parent}}{ {{prefixPartial "lime/LimeExternalDescriptor" " "}} +{{#if validatorName}} validator {{validatorName}}(){{/if}} {{>lime/LimeContainerContents}} } diff --git a/gluecodium/src/test/java/com/here/gluecodium/SmokeTest.kt b/gluecodium/src/test/java/com/here/gluecodium/SmokeTest.kt index ee5135f6d9..92f2100316 100644 --- a/gluecodium/src/test/java/com/here/gluecodium/SmokeTest.kt +++ b/gluecodium/src/test/java/com/here/gluecodium/SmokeTest.kt @@ -28,7 +28,6 @@ import org.junit.runners.Parameterized import org.junit.runners.Parameterized.Parameters @RunWith(Parameterized::class) - class SmokeTest( private val featureDirectory: File, generatorName: String, diff --git a/gluecodium/src/test/resources/smoke/validator/input/Validator.lime b/gluecodium/src/test/resources/smoke/validator/input/Validator.lime new file mode 100644 index 0000000000..32d3e5222f --- /dev/null +++ b/gluecodium/src/test/resources/smoke/validator/input/Validator.lime @@ -0,0 +1,29 @@ +# Copyright (C) 2016-2020 HERE Europe B.V. +# +# Licensed 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. +# +# SPDX-License-Identifier: Apache-2.0 +# License-Filename: LICENSE + +package smoke + +class ValidatedClass { + validator checkMe() + constructor makeMe() + + fun doStuff() + static fun doStaticStuff() + + property something: String + static property somethingStatic: String +} diff --git a/gluecodium/src/test/resources/smoke/validator/output/lime/smoke/ValidatedClass.lime b/gluecodium/src/test/resources/smoke/validator/output/lime/smoke/ValidatedClass.lime new file mode 100644 index 0000000000..0bdbece605 --- /dev/null +++ b/gluecodium/src/test/resources/smoke/validator/output/lime/smoke/ValidatedClass.lime @@ -0,0 +1,9 @@ +package smoke +class ValidatedClass { + validator checkMe() + constructor makeMe() + fun doStuff() + static fun doStaticStuff() + property something: String + static property somethingStatic: String +} diff --git a/lime-loader/src/main/antlr/LimeLexer.g4 b/lime-loader/src/main/antlr/LimeLexer.g4 index be8754ea5d..5b2f2d7ad3 100644 --- a/lime-loader/src/main/antlr/LimeLexer.g4 +++ b/lime-loader/src/main/antlr/LimeLexer.g4 @@ -79,6 +79,7 @@ Struct: 'struct' ; Throws: 'throws' ; TypeAlias: 'typealias' ; Types: 'types' ; +Validator: 'validator' ; // Predefined types diff --git a/lime-loader/src/main/antlr/LimeParser.g4 b/lime-loader/src/main/antlr/LimeParser.g4 index 376b265ee0..f95114c497 100644 --- a/lime-loader/src/main/antlr/LimeParser.g4 +++ b/lime-loader/src/main/antlr/LimeParser.g4 @@ -40,7 +40,7 @@ importStatement container : docComment* annotation* visibility? ('class' | 'interface') NewLine* simpleId NewLine* - (':' NewLine* identifier NewLine*)? '{' NewLine* externalDescriptor? + (':' NewLine* identifier NewLine*)? '{' NewLine* externalDescriptor? validator? ((function | constructor | property | struct | enumeration | constant | typealias | exception | lambda | container) NewLine*)* '}' NewLine+ ; @@ -73,6 +73,10 @@ throwsClause : 'throws' NewLine* docComment* typeRef NewLine* ; +validator + : 'validator' NewLine* simpleId NewLine* '(' NewLine* ')' NewLine* + ; + property : docComment* annotation* visibility? ('static' NewLine*)? 'property' NewLine* simpleId NewLine* ':' NewLine* typeRef NewLine* ('{' NewLine* getter (setter)? '}' )? NewLine+ diff --git a/lime-loader/src/main/java/com/here/gluecodium/loader/AntlrLimeModelBuilder.kt b/lime-loader/src/main/java/com/here/gluecodium/loader/AntlrLimeModelBuilder.kt index 4c6b99090c..1ba296667d 100644 --- a/lime-loader/src/main/java/com/here/gluecodium/loader/AntlrLimeModelBuilder.kt +++ b/lime-loader/src/main/java/com/here/gluecodium/loader/AntlrLimeModelBuilder.kt @@ -137,7 +137,8 @@ internal class AntlrLimeModelBuilder( exceptions = getPreviousResults(LimeException::class.java), classes = getPreviousResults(LimeClass::class.java), interfaces = getPreviousResults(LimeInterface::class.java), - lambdas = getPreviousResults(LimeLambda::class.java) + lambdas = getPreviousResults(LimeLambda::class.java), + validatorName = ctx.validator()?.simpleId()?.text ) } else { LimeInterface( diff --git a/lime-loader/src/main/java/com/here/gluecodium/loader/AntlrLimeModelBuilderBase.kt b/lime-loader/src/main/java/com/here/gluecodium/loader/AntlrLimeModelBuilderBase.kt index fb3d903c30..fab0933611 100644 --- a/lime-loader/src/main/java/com/here/gluecodium/loader/AntlrLimeModelBuilderBase.kt +++ b/lime-loader/src/main/java/com/here/gluecodium/loader/AntlrLimeModelBuilderBase.kt @@ -172,6 +172,10 @@ internal abstract class AntlrLimeModelBuilderBase( override fun exitExternalDescriptorValue(ctx: LimeParser.ExternalDescriptorValueContext?) {} + override fun enterValidator(ctx: LimeParser.ValidatorContext?) {} + + override fun exitValidator(ctx: LimeParser.ValidatorContext?) {} + override fun visitTerminal(node: TerminalNode) {} override fun visitErrorNode(node: ErrorNode) {} diff --git a/lime-runtime/src/main/java/com/here/gluecodium/model/lime/LimeClass.kt b/lime-runtime/src/main/java/com/here/gluecodium/model/lime/LimeClass.kt index 74a5518804..e951c6e596 100644 --- a/lime-runtime/src/main/java/com/here/gluecodium/model/lime/LimeClass.kt +++ b/lime-runtime/src/main/java/com/here/gluecodium/model/lime/LimeClass.kt @@ -19,6 +19,8 @@ package com.here.gluecodium.model.lime +import java.security.cert.CertPathValidator + class LimeClass( path: LimePath, visibility: LimeVisibility = LimeVisibility.PUBLIC, @@ -35,7 +37,8 @@ class LimeClass( classes: List = emptyList(), interfaces: List = emptyList(), lambdas: List = emptyList(), - parent: LimeTypeRef? = null + parent: LimeTypeRef? = null, + val validatorName: String? = null ) : LimeContainerWithInheritance( path = path, visibility = visibility, diff --git a/tools/Lime_IDL_file_type_for_IntelliJ.jar b/tools/Lime_IDL_file_type_for_IntelliJ.jar index 72a94b88b2bd5ea2144f33c24f3c4870be05c0bd..c1c1ca04d4ca39f3615466097f335c85a2fda444 100644 GIT binary patch delta 705 zcmdnZv5sScz8C`s!{OqNz^HD=pFB(q4C`1J7{nMDCY~3spBmy@JljBE@AGhjFVnc^ ze%oR3SpU+cTSt=L_~o6Ey0%TtDX4XWbA7G&pFnk4|6}YIZeOilyB<3Z&A({leup7Cl7ru)5!L#`?U7~Ta`e}Q~8yVyClx&vwWB5T9i_=?0)?E z|F%`<(mua1th3u}zdtTj{dA^TwU@Y0|E0|jsy;f-;(gwHk@fn`S$0qU_lTKmq%~Jg zxpTl@{IkW+X|69b40b=$;khSM^~33G-0el5{1!ZaF0`7f=-G~UXMf$vYSHOIBS#Hk^>3qiec13u<1};n%}LYlesTHG z=6liM)Y=tMUN3YUcmJP|aOmqbC(i4C@;|+9Jz3`!R?uI+!Srf>-SroI270G|akw9I z_|N`M;Fs~;x1U+{?{;r({x_p%=^SYtiN#OPaD3JkJ^u9YgTK83+m5f)vWs~4=*0$? zsR`^M|`_jpb!j+p1D#~9xG~T-Yme|&E5oY`85!WXeKmN9V+sV1x0?fEPOhqq5 z`_&4D^{sK@j4o2pTCBTYs=+kh*HH#%>)pC!vepGy zm&|<=&a!>;udQwG^814x#9iH|zRfH8RMDE3Uu0S2kNqq&JHJ?rwP)+J7OrDK`3YTp z39Bj-o2pk{Jh&s(f7k0@{ZpG~o|~9#yms3JtJCp3YpPTBreBlY^6s$l!P_S8%$ocQ zq%;@ibMA_5XAxc(&ZQdjyMFTh^Rr~4m!+`w{}kW7tnK1_w!3l1La)j^IU*tP?~r)Q zL#OY%tB);Rzo^7u&ZLb~J~yytKZ$D>K2shdGmXEw|GUEt*+r{UAup>PyWPY!z98mIfqG#2bjj7K!aiOK4weC(#aQ?6p8}88JXmm saiu^BuogxjxukIuki?Z9St02WEiDFkv$BB