<expression> expected, got '=' for variable assignment in Groovy - groovy

I've created following Groovy class in IntelliJ IDEA
1. package org.seleniumrun
2.
3. class Testing {
4. static void getJsonAsString(String endPointUrl) {
5. BufferedReader in = null;
6. }
7. .......
8. }
But it gives compilation error <expression> expected, got '=' at line number 6. I'm not getting how it is incorrect. Could you please help correct me if something is wrong here?

in is a reserved word used for loops. Rename it to something else (line 5) and its will work
Here is an example:
​class Testing {
static void getJsonAsString(String endPointUrl) {
BufferedReader reader= null;
println"hello: $endPointUrl"
}
}
def t = new Testing()
t.getJsonAsString(​"http://google.com")​​​​​​​​​
// and this is how "in" can be used
for( i in [1,2,3,4]) {
println i
}​
This is a working code that prints:
hello: http://google.com
1
2
3
4

Related

What causes "Variable does not exist in current context" message

I'm trying to write a very simple C# program in VS2015. I keep getting a message "The name (variable) does not exist in the current context." This is true for both my bool variable and string variable.
namespace PTouch
{
public class PTouch
{
bool lb_rc;
string strLabel;
lb_rc = false;
strLabel = "C:\BenchTop10\Standard 1in.lbx";
bpac.Document doc = new Document();
lb_rc = doc.Open("C:\BenchTop10\Standard 1in.lbx");
if lb_rc != false
{
doc.StartPrint("", 0);
doc.PrintOut(1, 0);
doc.EndPrint();
doc.Close();
}
else
{
MessageBox.Show("Open Error: " + doc.ErrorCode);
}
}
}
There are probably several problems with this code, but the first one is error about the variables not existing in the current context.
Any assistance is greatly appreciated.
I just discovered the problem... All the code has to be inside of a method, not just the class. I added a method to the class,
public void PrintLabel()
{ bool lb_rc; ... }
All errors cleared up, except MessageBox...
Thank you,
Tracy

Groovy: colon mark in closure?

I am trying to make my own Dsl and was playing around with different styles of closures in groovy.
I've stumbled upon follwing snippet:
myClosure {
testProperty: "hello!"
}
But can't figure out if this a valid code and how can I access then this testProperty. Is it valid? How can I read "hello!" value?
For now, lets put aside closures, consider the following code:
def f1() {
testProperty : 5
}
def f2() {
testProperty : "Hello"
}
println f1()
println f1().getClass()
println f2()
println f2().getClass()
This compiles (therefor the syntax is valid) and prints:
5
class java.lang.Integer
Hello
class java.lang.String
So what you see here is just a labeled statement (groovy supports labels see here)
And bottom line the code of f1 (just like f2) is:
def f1() {
return 5 // and return is optional, so we don't have to write it
}
With closures its just the same from this standpoint:
​def method(Closure c) {
def result = c.call()
println result
println result.getClass()
}
method {
test : "hello"
}
This prints
hello
class java.lang.String
as expected
Usually in DSL you have either this:
mySomething {
a = 42
b = 84
}
which corresponds to property setting
or this:
mySomething( a:42, b:84 ){
somethingElse{}
}
which is a method call with Map-literal.
The code you shown is not used as #mark-bramnik explained.

Groovy: Returning slash-escaped string causes exception

I have a simple script, which runs and works:
println testReturn()
String testReturn() {
def str = /asdf/
return str
}
If I change it to this, however, I see an error when I run it:
println testReturn()
String testReturn() {
return /asdf/
}
Error:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
test.groovy: 6: unexpected token: / # line 6, column 12.
return /asdf/
^
1 error
Does anyone know why I have to define a slash-escaped string before returning it?
It seems to me that the return statement is treated like other function calls in groovy 2.x and that slashy strings do not work well with paren-less function calls.
An example:
println /asdf/ // fails
println(/asdf/) // works
println testReturn()
String testReturn() {
/asdf/ //works - implicit return
}
String btestReturn() {
return /asdf/ // fails
}
String testReturn() {
return(/asdf/) //works
}
in my book this is unexpected behavior and a restriction of the groovy parser.
In groovy 3 (or more specifically in this case, groovy 3.0.0-alpha-3), the parser has been rewritten and the following code:
println /asdf/ // fails
println(/asdf/) // works
println testReturn1()
println testReturn2()
println testReturn3()
String testReturn1() {
/asdf/ // works
}
String testReturn2() {
return /asdf/ // works
}
String testReturn3() {
return(/asdf/) // works
}
gives the somewhat more informative error:
Caught: groovy.lang.MissingPropertyException: No such property: println for class: solution
groovy.lang.MissingPropertyException: No such property: println for class: solution
at solution.run(solution.groovy:1)
which tells us that without parens, groovy can not determine if println is a property or a method call. Removing that first line makes the above code work under groovy 3.x.

haxe abstract code not working for neko

The following code compiles to Javascript and runs OK, http://try.haxe.org/#8C940
abstract Comparable(Dynamic) from Float from String {
#:op(a<b) static function lt(a, b):Bool;
}
class Test {
public static function min<T:Comparable>(t:T, t2:T):T {
return (t:Comparable) < (t2:Comparable) ? t : t2;
}
static function main() {
var a = min(1.1,2.2); //ok
$type(a); //Float
trace(a); //1.1
var b = min(1,2); //ok
$type(b); //Int
trace(b); //1
var c = min("a","b"); //ok
$type(c); //String
trace(c); //a
//following will produce compilation error, correctly
//min(0, "a");
}
}
But when compiled for neko, it gives the following error:
Main.hx:7: characters 12-13 : Unexpected :
Main.hx:7: characters 12-13 : Unexpected :
Uncaught exception - load.c(181) : Module not found : main.n
The error in question is line:
return (t:Comparable) < (t2:Comparable) ? t : t2;
Any ideas why the language featuer works in one target but not the other? And how can I fix the issue for neko?
Thanks.
i guess you have a compiler version conflict, can you try the latest development builds:
http://hxbuilds.s3-website-us-east-1.amazonaws.com/builds/haxe/
(you can find the link here: http://haxe.org/manual/haxe3#git-builds -- edit: this page does not exist any more, but the build page is available via http://build.haxe.org)

ANTLR 4: Dynamic Token

Given:
grammar Hbs;
var: START_DELIM ID END_DELIM;
START_DELIM: '{{';
END_DELIM: '}}';
I'd like to know how to change START_DELIM and END_DELIM at runtime to be for example <% and %>.
Does anyone know how to do this in ANTLR 4?
Thanks.
There's a way, but you'll need to tie your grammar to a target language (as of now, the only target is Java).
Here's a quick demo (I included some comments to clarify things):
grammar T;
#lexer::members {
// Some default values
private String start = "<<";
private String end = ">>";
public TLexer(CharStream input, String start, String end) {
this(input);
this.start = start;
this.end = end;
}
boolean tryToken(String text) {
// See if `text` is ahead in the CharStream.
for(int i = 0; i < text.length(); i++) {
if(_input.LA(i + 1) != text.charAt(i)) {
// Nope, we didn't find `text`.
return false;
}
}
// Since we found the text, increase the CharStream's index.
_input.seek(_input.index() + text.length() - 1);
return true;
}
}
parse
: START ID END
;
START
: {tryToken(start)}? .
// The `.` is needed because a lexer rule must match at least 1 char.
;
END
: {tryToken(end)}? .
;
ID
: [a-zA-Z]+
;
SPACE
: [ \t\r\n] -> skip
;
The { ... }? is a semantic predicate. See: https://github.com/antlr/antlr4/blob/master/doc/predicates.md
Here's a small test class:
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.*;
public class Main {
private static void test(TLexer lexer) throws Exception {
TParser parser = new TParser(new CommonTokenStream(lexer));
ParseTree tree = parser.parse();
System.out.println(tree.toStringTree(parser));
}
public static void main(String[] args) throws Exception {
// Test with the default START and END.
test(new TLexer(new ANTLRInputStream("<< foo >>")));
// Test with a custom START and END.
test(new TLexer(new ANTLRInputStream("<? foo ?>"), "<?", "?>"));
}
}
Run the demo as follows:
*nix
java -jar antlr-4.0-complete.jar T.g4
javac -cp .:antlr-4.0-complete.jar *.java
java -cp .:antlr-4.0-complete.jar Main
Windows
java -jar antlr-4.0-complete.jar T.g4
javac -cp .;antlr-4.0-complete.jar *.java
java -cp .;antlr-4.0-complete.jar Main
And you'll see the following being printed to the console:
(parse << foo >>)
(parse <? foo ?>)

Resources