Velocity parse error on a null value - parsing

I have a problem in my velocity template.
I have to show the image url for some products.
I set a variable in a right way. In some cases anyway I don't have this image, so I have to hide the image and put a blank space in the template.
I write the set variable in this way:
#set ($variantUrl = ${#if(!$product.getOrderFormImage().getUrl()) $!product.getOrderFormImage().getUrl() #else $product.getOrderFormImage().getUrl() #end} )
but I obtain a parse error:
Caused by: org.apache.velocity.exception.ParseErrorException:
Encountered "(" at
de.hybris.platform.commons.renderer.impl.VelocityTemplateRe
nderer[line 403, column 103] Was expecting:
...
I don't see any error in this line. What's the problem?

I'm not sure you can nest #if #else within #set - at least there's no mention of it in the Velocity user guide.
When I use your original expression I get the following error:
org.apache.velocity.exception.ParseErrorException: Encountered "#if" at ....
Was expecting:
<IDENTIFIER> ...
If I rewrite to make the #if #else to be the top-level statement such as:
#if( !$product.getOrderFormImage().getUrl() )
#set($variantUrl = $!product.getOrderFormImage().getUrl() )
#else
#set($variantUrl = $product.getOrderFormImage().getUrl() )
#end
this seems to do what you want, or at least it compiles and doesn't error! It's also a lot easier to read and understand.
Personally, I'd go one step further for the purpose of readability and use Velocity's shorthand notation for references:
#if( !$product.orderFormImage.url )
#set($variantUrl = $!product.orderFormImage.url )
#else
#set($variantUrl = $product.orderFormImage.url )
#end

Related

roblox LUA Expected 'end' (to close 'than' at line 3), got '='

function teleportTo(placeCFrame)
local plyr = game.Players.LocalPlayer;
if plyr.Character then
return plyr.Character.HumanoidRootPart.CFrame = placeCFrame;
end
end
teleportTo(game:GetService("Workspace").game:GetService("Workspace").Zeppelin.FuelTank1.Tank.CFrame)
my code is here, idk much about coding thanks for helping
and if you told me how to make the player teleport to a moving object it would be so super
The error is telling you what is going on.
When the code was being interpreted line by line, it expected the next symbol to be end, but instead it got =.
That means that something about how you're using the equals sign is incorrect. So when we look at the line :
return plyr.Character.HumanoidRootPart.CFrame = placeCFrame
You cannot assign a value on the same line as a return command. return is used to pipe a value out of a function so it can be used where the function was called.
But I'm pretty sure that isn't what you intended, you just want to set a player's position. So to fix your issue, remove the return.
if plyr.Character then
plyr.Character.HumanoidRootPart.CFrame = placeCFrame
end
The reason you are getting an error is because = is usually defined as setting a value, and no code can be executed after a return or it will error
If you wanted to, you could add return after all the code is done executing

Embedding mRuby: retrieving mrb_parser_message after parse error

I'm trying to embed mRuby in a Max MSP object. One of the first things I want to setup is error logging in the Max IDE console window. To that effect, after I parse the code ( stored in a C string ) with mrb_parse_string, I expect errors to be available in the parser's error_buffer array, but the structures in this array are always empty ( lineno and column set to 0 and message set to NULL ) even when there is an error.
Is there a special way to set up the parser before parsing the code so it fills its error_buffer array properly in case an error occurs ? I've looked into the mirb source, but it doesn't look like it. I'm lost. Here is the code I'm using, taken from a small C program I'm using as test:
mrb_state *mrb;
char *code;
struct mrb_parser_state *parser;
parser = mrb_parse_string(mrb, code, mrbc_context_new(mrb));
if (parser->nerr > 0) {
for(i = 0; i < parser->nerr; i++) {
printf("line %d:%d: %s\n", parser->error_buffer[i].lineno,
parser->error_buffer[i].column,
parser->error_buffer[i].message);
}
return -1;
}
When passed the following faulty ruby code:
[1,1,1]]
the previous code outputs :
line 1:8: syntax error, unexpected ']', expecting $end
line 0:0: (null)
I don't know where the first line comes from, since I compiled mRuby with MRB_DISABLE_STDIO defined and as line 14 and following in mrbconf.md suggests, but it is accurate.
The second line is the actual output from my code and shows that the returned mrb_parser_state structure's error_buffer is empty, which is surprising since the parser did see an error.
Sorry totally misunderstood your question.
So you want to:
capture script's syntax errors instead of printing.
make MRB_DISABLE_STDIO work.
For 1st issue
struct mrb_parser_state *parser;
parser = mrb_parse_string(mrb, code, mrbc_context_new(mrb));
should be replaced with:
struct mrbc_context *cxt;
struct mrb_parser_state *parser;
cxt = mrbc_context_new(mrb);
cxt->capture_errors = TRUE;
parser = mrb_parse_string(mrb, code, cxt);
like what mirb does.
For 2nd issue I don't know your build_config.rb so I can't say much about it.
Some notes to make things accurate:
MRB_DISABLE_STDIO is a compile flag for building mruby so you need to pass it in build_config.rb like:
cc.defines << %w(MRB_DISABLE_STDIO)
(see build_config_ArduinoDue.rb)
line 1:8: syntax error, unexpected ']', expecting $end
is the parsing error of mruby parser([1,1,1]] must be [1,1,1]).
And 1:8 means 8th column of 1st line (which points to unnecessary ]) so it seems like your C code is working correctly to me.
(For a reference your code's compilation error in CRuby:
https://wandbox.org/permlink/KRIlW2956TnS6puD )
prog.rb:1: syntax error, unexpected ']', expecting end-of-input
[1,1,1]]
^

strange errors when using parsertree

I am playing around with the parsing of code using the java15 syntax.
I noticed that when parsing an entire class it gives me an error if the class file ends with an empty line. I wrote some code to remove these empty lines before parsing but is there a more structural solution? Or am I missing something?
Related: when I am trying to parse a single method: as soon as I change something to the location of the accolades { } ( on a separate line or not for example) I receive an error.
|std:///ParseTree.rsc|(14967,5,<455,80>,<455,85>): ParseError(|java+class:///smallsql/database/language/Language_it|(10537,0,<152,0>,<152,0>))
at parse(|std:///ParseTree.rsc|(14967,5,<455,80>,<455,85>))
at $root$(|prompt:///|(0,7,<1,0>,<1,7>))
at *** somewhere ***(|std:///ParseTree.rsc|(14967,5,<455,80>,<455,85>))
at parse(|std:///ParseTree.rsc|(14967,5,<455,80>,<455,85>))
at $root$(|prompt:///|(0,7,<1,0>,<1,7>))
I'm guessing you are parsing using code like so:
parse(#CompilationUnit, file)
[CompilationUnit] file
But: a CompilationUnit does not start or end with layout notation.
To solve this, you should declare the start non-terminal of a grammar like so:
start syntax CompilationUnit = ... ;
layout L = [\t\n\r\ ]*; // it's necessary to have L be nullable
and this will (internally and hidden) generate for you a new production, like so:
syntax start[CompilationUnit] = L CompilationUnit top L;
With this, you can then parse a whole file which might end with layout:
parse(#start[CompilationUnit], file)
[start[CompilationUnit]] file
To extract the CompilationUnit the top field comes in handy:
CompilationUnit u = parse(#CompilationUnit, file).top

How to fix JSLint insecure ^ error?

I have following function. What this does is filters character that is allowed in a subdomain. In JSLint I got following error. Is there any way I can do this without JSLint showing error. I know I can ignore error in JSLint settings but Is there any other way I can improve my code to not show JSLint error.
function filterSubDomain(value) {
return value.replace(/[^a-z0-9\-]/ig, '')
.replace(/^[\-]*/, '')
.replace(/[\-]*$/, '')
.toLowerCase();
}
I think this is easy enough -- just a quick head rethread. I'll repeat the comment from above quickly: JSLint wants you to say what you do want rather than what you don't, because saying what you don't want always leaves room for a superset of what you do want to sneak in. That is, JSLint's intention is to force you to code explicitly/precisely.
So instead of replace, you want to use match here. Here's one way, I believe (stealing from MDN's match code a little):
/*jslint sloppy:true, white:true, devel:true */
function filterSubDomain(value) {
var out = value,
re = /[a-z0-9\-]+/gi,
found;
found = value.match(re);
out = found.join("");
// There are better ways to `trim('-')`.
while (0 === out.indexOf("-")) {
out = out.substr(1);
}
while (out.length === out.lastIndexOf("-")+1) {
out = out.slice(0,out.length-1);
}
return out;
}
console.log(filterSubDomain('---For more inform--ation, - see Chapter 3.4.5.1---'));
// Formoreinform--ation-seeChapter3451
There are other ways to trim, but you get the point. No not's in JavaScript regular expressions with JSLint!

What does an identifier followed by a colon mean in Objective-C?

bail:
if ( err && image ) {
CGImageRelease( image );
image = NULL;
}
if ( provider ) CGDataProviderRelease( provider );
if ( colorspace ) CGColorSpaceRelease( colorspace );
*imageOut = image;
return err;
I looked at some code and found this. I have never seen this before. What does bail: mean?
It comes from here.
It's a label that the goto statement jumps to.
The code you're looking at, SquareCamViewController.m, uses a macro named require, like this:
require( error == nil, bail );
This macro is defined in the AssertMacros.h header file. It takes a label as its second argument, and uses goto if the first argument evaluates to false.
Using goto to jump to cleanup code at the end of a function is the most common use of goto and labels in C.
bail: is a label. This is standard C syntax. It's not used very often in properly written code. It's most common use is with goto. Please avoid using goto. In the code you referenced it is used by the require function. If the require fails, the code will jump ahead to the bail label, skipping all of the other code in between.

Resources