How does SystemVerilog compiler knows to separate two arguments in a macro? - macros

I have this macro:
`define do_code(DO_SOETHING, ID) \
fork \
begin \
``DO_SOMETHING`` \
end \
begin \
$display("%s",ID.name()); \
end \
join_any \
disable fork; \
and I use it as such:
`do_code($display("%s",argA.name()), argB)
How does the compiler knows to separate the two macro's input arguments correct:
DO_SOMETHING = $display("%s",argA.name())
ID = argB
Why not break it to:
DO_SOMETHING = $display("%s"
ID = argA.name()), argB
???

The compiler knows because the IEEE 1800-2017 SystemVerilog LRM
says in section 22.5.1 `define
Actual arguments and defaults shall not contain comma or
right parenthesis characters outside matched pairs of left and right parentheses (), square brackets [],
braces {}, double quotes "", or an escaped identifier.
The comma in your fist argument is inside a matched pair of parenthesis.
BTW, you should not be using `` unless you are trying to create a new identifier by joining macro argument with text in the body of the macro,

Related

Scala replace multiple strings having common character at once

val str= " This string has " , need to escape with \ .Even string has \ before"
val resultShouldbe=" This string has \" ,need to escape with \\.Even string has \\ before"
str.replace(""""""" , """\"""").replace("\\","\\\\")
The output of first replace is adding up to the second replace.
Kindly help.
str.replaceAll("([\"\\\\])" , "\\\\$1")
Matching regex:
(...) - capture group: Capture everything that matches this pattern.
[...] - character class: Match any of the given characters.
\"\\\\ - 2 characters: A quote mark (escaped) or a backslash (doubly escaped).
Replacement string:
\\\\$1 - 2 elements: A backslash (doubly escaped) followed by whatever was captured in the 1st capture group. (In this case there was only 1 capture group.)
In other words: For every quote " or backslash \ character, replace it with the same character preceded by a backslash \ character.

Split string Swift without introducing Newlines

In Swift, when you want to separate a string in multiple lines, because is too long, you can do this:
let string = """
A
B
C
D
E
"""
But this method introduces Newline between each line:
print(string)
// Prints: A\nB\nC\nD\nE
Is it possible to separate a very long string in multiple lines (for code visibility reasons), avoiding Newlines?
End a line with a backslash to suppress the subsequent newline. Like this:
let string = """
A \
B \
C \
D \
E
"""

Running a PowerShell script file with path containing spaces from Jenkins Pipeline without using backtick

I want to run the following PowerShell script file from Jenkins Pipeline:
".\Folder With Spaces\script.ps1"
I have been able to do it with the following step definition:
powershell(script: '.\\Folder` With` Spaces\\script.ps1')
So I have to remember to:
escape the backslash with a double backslash (Groovy syntax)
escape the space with backtick (PowerShell syntax)
I would prefer to avoid at least some of this. Is it possible to avoid using the backtick escaping, for example? (Putting it between "" does not seem to work, for some reason.)
I found that it's possible to use the ampersand, or invoke, operator, like this:
powershell(script: "& '.\\Folder With Spaces\\script.ps1'")
That gets rid of the backtick escaping, and should make life a tiny bit easier.
To avoid escaping the backslashes you could use slashy strings or dollar slashy strings as follows. However you cannot use a backslash as the very last character in slashy strings as it would escape the /. Of course slashes as well would have to be escaped when using slashy strings.
String slashy = /String with \ /
echo slashy
assert slashy == 'String with \\ '
// won't work
// String slashy = /String with \/
String dollarSlashy = $/String with / and \/$
echo dollarSlashy
assert dollarSlashy == 'String with / and \\'
And of course you'll lose the possibility to include newlines \n and other special characters in the string using the \. However as both slashy and dollar slashy strings have multi line support at least newlines can be included like:
String slashyWithNewline = /String with \/ and \
with newline/
echo slashyWithNewline
assert slashyWithNewline == 'String with / and \\ \nwith newline'
String dollarSlashyWithNewline = $/String with / and \
with newline/$
echo dollarSlashyWithNewline
assert dollarSlashyWithNewline == 'String with / and \\ \nwith newline'
If you combine that with your very own answer you won't need both of the escaping.

XText cross referencing

I have written following grammar
Model:
package = PackageDec?
greetings+=Greeting*
usage+=Usage* ;
PackageDec:
'package' name=QualifiedName ;
Greeting:
'greet' name=ID '{' ops += Operation* '}' ;
Operation:
'op' name=ID ('(' ')' '{' '}')? ;
QualifiedName:
ID ('.' ID)*;
Usage:
'use';
With above i can write following script.
package p1.p2
greet G1 {op f1 op f2 }
Now i need to write something like this:
package p1.p2
greet G1 {op f1 op f2 op f3}
use p1.p2.G1.f1
use p1.p2.G1
use p1.p2.G1.f3
To support that i changed Usage RULE like this
Usage:
'use' head=[Greet|QualifiedName] =>('.' tail=[Operation])?
However when i generate xtext artifacts it is complaining about multiple alternatives.
Please let me know how to write correct grammar rule for this.
This is because QualifiedName consumes dots (.). Adding ('.' ...)? makes two alternatives. Consider input
a.b.c
This could be parsed as
head="a" tail = "b.c"
head="a.b" tail = "c"
If I understand your intention of using predicate => right, than you just have to replace
head=[Greet|QualifiedName]
with
head=[Greet]
In this case however you will not be able to parse references with dots.
As a solution I would recommend to substitute your dot with some other character. For example with colon:
Usage:
'use' head=[Greet|QualifiedName] (':' tail=[Operation])?

How to parse variable in REXX/Regina

When writing a macro, I have a variable, {$#2}, that either starts with a 1 or 2 digit number followed by a ".", or it doesn't. When it starts in that way, I want to put the number into a rexxvar, which I have called C.
I have tried
#if [ DATATYPE(LEFT({$#2},1), "W") ] \
#evaluate ^^ ^parse '{$#2} C '.' .^ \
#endif \
This, and every variation I can think of, gives errors saying the #IF line contains invalid characters.
How should I do this?
I am using PPWizard, and Regina - but I can't create either tag.
Thanks
Ian
I do not use PPWizard so this could all be wrong, but
Looking at PPWizard #if, the if should be either
#if DATATYPE(LEFT({$#2},1), "W")
or
#if [ DATATYPE(LEFT({$#2},1), "W") <> 0 ]
But I do not know wether you can imbed the {$#2} or not (I do not know PPWizard)
For the parse statement one of these may be what you want
#evaluate ^^ ^parse value '{$#2}' with C '.' .^
or
#evaluate ^^ ^parse var {$#2} C '.' .^
See rexx parse syntax
An alternative way might try the Define Rexx tag and do it in rexx. More people could help you with pure rexx. i.e the rexx would be
if DATATYPE(LEFT(value_to_process,1), "W") then do
parse var value_to_process C '.'
end
where value_to_process is the value to be checked (i.e. {$#2})
For others.
The final answer to the problem is to write
#RexxVar value_to_process = {$#2}
#evaluate+ ^^ ^if DATATYPE(LEFT(value_to_process,1), "W") then do; parse var value_to_process C '.'; end^ \
in the macro I was creating.
Thanks Bruce. Your reply was most helpful on my route to a solution.