In JavaCC, how can I match an "invalid" token to throw exception only, without counting it as expected token? - javacc

In my parser, at a point only "a" or "b" is valid and expected, while everything else is not (including "someSpecialUnexpectedThing"). However, I want to throw a special exception MyOwnException when I meet "someSpecialUnexpectedThing" so I write my parser like this:
MyResult parse() :
{
"a"
{...}
|
"b"
{...}
|
"someSpecialUnexpectedThing"
{
throw new MyOwnException();
}
}
There is a problem that, in this case when I get "c", the error message will be: Encountered "c", was expecting one of: "a", "b", "someSpecialUnexpectedThing", but I don't want to mislead user that "someSpecialUnexpectedThing" is a valid input here.
How should I achieve this?
If I write the parser as:
MyResult parse() :
{
"a"
{...}
|
"b"
{...}
}
then if it encounters someSpecialUnexpectedThing, it will throw regular ParseException that it's expecting "a" or "b", instead my own exception which I do need.
Thanks!

Related

How to access a Python dictionary name with string?

I have string "dict_1", and I have dictionary named dict_1.
x = "dict_1"
dict_1 = {"a": "b", "c": "d"}
So, can I access dict_1 like x['a']?
I found a Stack Overflow Question, but in this question, the OP wants to create variable with string, not access variable (dictionary) with string.
I really need some help. I'm using Python 3.8 and Windows.
Thanks!
The way I see it, you have two basic options here.
The most obvious way is to use eval to convert the value of the string x into a usable Python object - in this case the dictionary dict_1.
Note that eval executes a string as Python code. If the string is coming from an untrusted source, then this is quite dangerous to do. You should use this with caution, and apply appropriate sanitization to any inputs.
x = "dict_1"
dict_1 = {"a": "b", "c": "d"}
print(type(eval(x)))
# Prints <class 'dict'>
print(eval(x)['a'])
# Prints "b"
The other method is to make use of locals(). This returns a dictionary of variables defined in the local scope. You would index it with x, which would give you the dictionary itself as with eval.
x = "dict_1"
dict_1 = {"a": "b", "c": "d"}
print(type(locals()[x]))
# Prints <class 'dict'>
print(locals()[x]['a'])
# Prints "b"
In theory, using locals() for the job would prove to be safer since with eval, you may inadvertently execute arbitrary code.
Once again, if coming from an untrusted source, you should do some basic sanity checks, e.g. check that the variable is in scope and is a dictionary:
if x in locals() and type(locals()[x]) == dict:
print(locals()[x]['a'])
# Prints "b"

swift 4 set subkey of dictionary

I would like to set the value of a key of a key in swift 4. Is this possible?
For example if we have
var d:[String:Any] = [:]
I see there is a function 'updateValue'
d.updateValue([:], forKey: "a")
But what I want to be able to do is:
d.updateValue([:], forKey: "a.b.c")
which would be expected to resolve to
{"a" : { "b" : { "c" : {} }
instead of
{"a.b.c" : {} }
thanks
Dictionary does not support this out the box but you could use keypaths.
https://oleb.net/blog/2017/01/dictionary-key-paths/
Your best bet is to create nested dictionaries or a custom JSON type. https://stackoverflow.com/a/25475702/3705470

Argument names function R

I am trying to create a function, and I want to be able to get the names of my arguments , and I used formalArgs (I found it as answer to a question like mine) for that but I don't get what I want (I even tried formals()).
This is my function
entropy= function(...)
{
rest of the code
print(formalArgs(entropy))
}
when I call the function entropy(h1,h2,h3)
the formalArgs prints
$...
but I want to be able to get
h1,h2,h3
is there a way to do that ?
thanks :)
Use substitute() to fetch the unaltered input and deparse it.
entropy= function(h1,h2,h3)
{
#rest of the code
print(c( deparse(substitute(h1)), deparse(substitute(h2)), deparse(substitute(h3))))
}
entropy(a,b,c)
#[1] "a" "b" "c"

Select property from each object in a list

Say I have a List<Tuple>, where the first element in each is a string. Is there an extension function in Kotlin to select the first element from each of these tuples?
I'm looking for something like the C# LINQ syntax for Select:
myTuples.Select(t => t.item1)
In Kotlin, a Tuple could be a Pair or a Triple. You could just map over the list and select out the first element, like this:
val myTuples : List<Triple<String,String,String>> = listOf(
Triple("A", "B", "C"),
Triple("D", "E", "F")
)
val myFirstElements: List<String> = myTuples.map { it.first } // ["A", "D"]
And of course, you can leave off the types, I've left them in to make this easier to follow.

Why is the meteor shell not giving me a return value for this function call?

For this particular function call, I'm not seeing any return values. I want to know what the return value is so I can determine whether a record was found or not.
> Rooms.findOne({name: "this record doesn't exist"})
> null //me entering 'null' manually
null
> []
[]
> "test"
'test'
> Rooms.findOne({name: "this record doesn't exist either"})
>
Trying with typeof gives:
> typeof Rooms.findOne({name: "this record doesn't exist"})
> 'undefined'
Which means meteor server-shell somehow does not return undefined! looking into source code it could be caused here https://github.com/meteor/meteor/blob/release-1.1/tools/server/shell-server.js#L253
Not sure why, but try x = rooms.findone and then type x to see its value.

Resources