Discussion:
[j-nsp] negation operator in SLAX
Martin T
2018-06-14 21:39:30 UTC
Permalink
Hi!

I have quite often used "!" negation operator familiar from other
languages. For example:

/* If string does not match the pattern, then terminate the script. */
if ( ! jcs:regex( $pattern, $string ) ) {
terminate 'Invalid input string!';
}

However, I have not found this method in the official SLAX
documentation or SLAX operators list. Based on my example above, the
suggested solution seems to be to check if jcs:regex returned an empty
node-set or not using jcs:empty:

if ( jcs:empty( jcs:regex( $pattern, $string ) ) ) {
terminate 'Invalid input string!';
}

Just out of curiosity, is there a difference between those two methods?


thanks,
Martin
_______________________________________________
juniper-nsp mailing list juniper-***@puck.nether.net
https://puck.nether.net/mailman/listinfo/juniper-nsp
Timur Maryin via juniper-nsp
2018-06-15 08:00:11 UTC
Permalink
Hi Martin,


There is not() :


https://www.juniper.net/documentation/en_US/junos/topics/reference/scripting/junos-script-automation-function-xslt-not.html
Post by Martin T
Hi!
I have quite often used "!" negation operator familiar from other
/* If string does not match the pattern, then terminate the script. */
if ( ! jcs:regex( $pattern, $string ) ) {
terminate 'Invalid input string!';
}
However, I have not found this method in the official SLAX
documentation or SLAX operators list. Based on my example above, the
suggested solution seems to be to check if jcs:regex returned an empty
if ( jcs:empty( jcs:regex( $pattern, $string ) ) ) {
terminate 'Invalid input string!';
}
Just out of curiosity, is there a difference between those two methods?
_______________________________________________
juniper-nsp mailing list juniper-***@puck.nether.net
https://puck.nether.net/mailman/listinfo/juniper-nsp
Martin T
2018-06-16 17:58:59 UTC
Permalink
Hi!
Oh, yes. I forgot the not() function. I made a small comparison
between not(), jcs:empty() and ! using five data types in SLAX:


1) False boolean value:
As expected "not( $boolean )" returns true. Also, the "jcs:empty(
$boolean )" returns true because jcs:empty() always consideres boolean
data type to be empty. Also, the "! $boolean" returns true. So for
boolean data type everything works as expected.

2) Empty string:
As expected, "not( $string )" returns true. As string is blank, then
"jcs:empty( $string )" also returns true. So does "! $string".

3) Number 0:
Again, "not( $number )", "jcs:empty( $number )" and "! $number" all
return true as expected.

4) Empty node-set:
Again, "not( $node-set )", "jcs:empty( $node-set )" and "! $node-set"
return true.

5) Empty RTF:
"not( $rtf )" returns false, which is an expected result because even
an empty RTF converts to true. "jcs:empty( $rtf )", to my surprise,
returns true. According to documentation, RTF is always considered to
not be empty. "! $rtf" returns false.


So I guess it is safe to say, that "!" is the same as "not()" function?


thanks,
Martin
_______________________________________________
juniper-nsp mailing list juniper-***@puck.nether.net
https://puck.nether.net/mailman/listinfo/juniper-nsp
Phil Shafer
2018-06-18 18:40:23 UTC
Permalink
Post by Martin T
Oh, yes. I forgot the not() function. I made a small comparison
"!" and "not" are identical. The "!" is just syntactic sugar that
turns "! x " into "not(x)", as you can see in the code:

https://github.com/Juniper/libslax/blob/master/libslax/slaxparser-xp.y#L164
Post by Martin T
"not( $rtf )" returns false, which is an expected result because even
an empty RTF converts to true. "jcs:empty( $rtf )", to my surprise,
returns true. According to documentation, RTF is always considered to
not be empty. "! $rtf" returns false.
RTFs are exactl the reason I made jcs:empty(), since it bugs me
that boolean($rtf) is true.

http://juniper.github.io/libslax/slax-manual.html#slaxis-empty

And "bugs" is too weak a word. Imho, RTFs are the chief source of
slax/xslt violations of the Principal of Least Astonishment.

% cat /tmp/foo.slax
version 1.2;

var $a := <a> {
<b> "b";
}

main <top> {
var $x = { copy-of $a/z; }
var $y = $a/z;

<x> boolean($x);
<y> boolean($y);
}
% slaxproc -g -E /tmp/foo.slax
<?xml version="1.0"?>
<top>
<x>true</x>
<y>false</y>
</top>

See also the ":=" assignment operator, which helps avoid RTFs:

http://juniper.github.io/libslax/slax-manual.html#colon-equals

Thanks,
Phil
_______________________________________________
juniper-nsp mailing list juniper-***@puck.nether.net
https://puck.nether.net/mailman/listinfo/juniper-nsp
Martin T
2018-06-19 08:44:26 UTC
Permalink
Post by Phil Shafer
Post by Martin T
Oh, yes. I forgot the not() function. I made a small comparison
"!" and "not" are identical. The "!" is just syntactic sugar that
https://github.com/Juniper/libslax/blob/master/libslax/slaxparser-xp.y#L164
Post by Martin T
"not( $rtf )" returns false, which is an expected result because even
an empty RTF converts to true. "jcs:empty( $rtf )", to my surprise,
returns true. According to documentation, RTF is always considered to
not be empty. "! $rtf" returns false.
RTFs are exactl the reason I made jcs:empty(), since it bugs me
that boolean($rtf) is true.
http://juniper.github.io/libslax/slax-manual.html#slaxis-empty
And "bugs" is too weak a word. Imho, RTFs are the chief source of
slax/xslt violations of the Principal of Least Astonishment.
% cat /tmp/foo.slax
version 1.2;
var $a := <a> {
<b> "b";
}
main <top> {
var $x = { copy-of $a/z; }
var $y = $a/z;
<x> boolean($x);
<y> boolean($y);
}
% slaxproc -g -E /tmp/foo.slax
<?xml version="1.0"?>
<top>
<x>true</x>
<y>false</y>
</top>
http://juniper.github.io/libslax/slax-manual.html#colon-equals
Thanks,
Phil
Again, thank you for explaining this Phil!


Martin
_______________________________________________
juniper-nsp mailing list juniper-***@puck.nether.net
https://puck.nether.net/mailman/listinfo/juniper-nsp
Timur Maryin via juniper-nsp
2018-07-03 09:31:31 UTC
Permalink
Hi Phil,
Post by Phil Shafer
"!" and "not" are identical. The "!" is just syntactic sugar that
Was it always like this?

_______________________________________________
juniper-nsp mailing list juniper-***@puck.nether.net
https://puck.nether.net/mailman/listinfo/juniper-nsp
Phil Shafer
2018-07-03 20:32:40 UTC
Permalink
Post by Timur Maryin via juniper-nsp
Post by Phil Shafer
"!" and "not" are identical. The "!" is just syntactic sugar that
Was it always like this?
Yup. SLAX is all about syntactic sugar. Well, mostly, anyway.
There are extension functions and mutable variables also.

Thanks,
Phil
_______________________________________________
juniper-nsp mailing list juniper-***@puck.nether.net
https://puck.nether.net/mailman/listinfo/juniper-nsp

Loading...