Using "~[]" token with lexical states - javacc

I'm trying to write a javacc-based parser that involves the following tokens / lexical states:
TOKEN :
{
<"{"> : FIRST
}
<FIRST, DEFAULT> TOKEN :
{
<"~[]"> : DEFAULT
}
Trying to parse "{;}" results in the lexical error
Encountered: ";" (59), after : ""
which I don't understand. I can avoid the error in two ways:
by replacing the "~[]" pattern by an explicit ";" literal
by removing the FIRST lexical state
However, I do need both of these (as you can guess, the above is just a minimal test case), so this isn't a suitable workaround. Any idea what is wrong with the above token definition ?
Thanks !

Too many quote marks. What you want is
TOKEN :
{
<"{"> : FIRST
}
<FIRST, DEFAULT> TOKEN :
{
<~[]> : DEFAULT
}

Related

Cannot call `JSON.parse` with `localStorage.getItem(...)`

Just added Flow types to a project I'm working on and progressively adding types until I got to this error:
Cannot call JSON.parse with localStorage.getItem(...) bound to text because null or undefined [1] is incompatible with
string [2]
This comes from a expression:
const myVar = JSON.parse(localStorage.getItem('itemName'))
I understand why I get this error (except maybe the "bound to text" part), but couldn't find a way around it. I'd appreciate any help here!
So, the function localStorage.getItem can return null values and flow wants you to tackle them before parsing it. As JSON.parse only takes a string, you can do the following:
localStorage.getItem("key") || '{}'
So, if it returns null. The empty object string is chosen, which JSON.parse can parse into an empty object.
Prefer using 'null' than '{}' as it parses to empty object
JSON.parse(localStorage.getItem("key") || 'null') // null
JSON.parse(localStorage.getItem("key") || '{}') // {} - empty object

how to guard against an Implicitly Unwrapped Optional causing an "unexpectedly found nil" error with Firebase and then give it a value

I am getting the error Fatal error: Unexpectedly found nil while unwrapping an optional value at my line
let pictureRef = self.actStorage.child("\(user.uid).jpg")
I know that this is caused by implicitly trying to unwrap an optional which is my
var actStorage: StorageReference!
my question is how would I use optional binding or a guard statement so that it is no longer nil?
If Swift can compile the code without inserting an implicit unwrap, it does so. You can change the code so it doesn't need an implicit unwrap:
guard let pictureRef = self.actStorage?.child("\(user.uid).jpg") else {
return
}
However, it might be better to change the type of actStorage to an explicit optional (StorageReference?) instead, if you can change the type.
This will do:
if let pictureRef = self.actStorage?.child("\(user.uid).jpg") {
// pictureRef exists
} else {
// picutreRef nill
}

If property exists logic in BizTalk message assignment shape

Is the if / else logic valid in a BizTalk Message Assignment shape?
I'm getting some event log errors regarding ErrorReport.FailedTime having no value, so I thought I'd put a guard clause in the
if (ErrorReport.FailureTime exists Msg_Failed)
{
Var_FailureTime = Msg_Failed(ErrorReport.FailureTime);
}
else
{
Var_FailureTime = System.DateTime.Now;
}
... rest of code constructing the error report message ...
But the compiler fails with ...
error X2254: unexpected keyword: 'if'
That is the expected behavior.
'If' is not supported in the Message Assignment Shape but it is supported in the Expression Shape. so, you will have to do this test/assigment before the Construct Shape.

qRegisterMetaType usage

#include<QMetaType>
typedef QList<int> IntList;
qRegisterMetaType<IntList>("IntList");
error C2909: 'qRegisterMetaType': explicit instantiation of function template requires return type
C2909 says I need to define
template int qRegisterMetaType<IntList>("IntList");
If I define like I mentioned above then I get the below error
error C2059: syntax error : 'string'
warning C4667: 'int qRegisterMetaType(void)' : no function template defined that matches forced instantiation
why do I get this error ?
"qRegisterMetaType" is a function. It must appear in a code block.
int metatype_id = qRegisterMetaType<IntList>("IntList");
You need to add Q_DECLARE_METATYPE(IntList) before you can register it.

Recovering multiple errors in Javacc

Is there a way in javacc to parse an input file further even after detecting an error. I got to know that there are several ways such as panic mode recovery, phrase level recovery and so on. But I can't figure how to implement it in javacc jjt file.
For an example assume my input file is
Line 1: int i
Line 2: int x;
Line 3: int k
So what I want is after detecting the error of missing semicolon at line 1, proceed parsing and find the error at line 3 too.
I found the answer in the way of panic mode error recovery,but it too have some bugs. What I did was I edit my grammar so that once I encounter a missing character in a line of the input file(in the above case a semicolon) parser proceed until it finds a similar character. Those similar characters are called synchronizing tokens.
See the example below.
First I replaced all the SEMICOLON tokens in my grammar with this.
Semicolon()
Then add this new production rule.
void Semicolon() :
{}
{
try
{
<SEMICOLON>
} catch (ParseException e) {
Token t;
System.out.println(e.toString());
do {
t = getNextToken();
} while (t.kind != SEMICOLON && t!=null && t.kind != EOF );
}
}
Once I encounter a missing character parser search for a similar character.When it finds such character it returns to the rule which called it.
Example:-
Assume a semicolon missing in a variable declaration.
int a=10 <--- no semicolon
So parser search for a semicolon.At some point it finds a semicolon.
___(some code)__; method(param1);
So after finding the first semicolon in the above example it returns to the variable declaration rule(because it is the one which called the semicolon() method.) But what we find after the newly find semicolon is a function call,not a variable declaration.
Can anyone please suggest a way to solve this problem.

Resources