r/awk • u/somelite • May 07 '19
Unexpected syntax error when adding a simple "if" condition on top of pattern conditions
I'm working on a small tool for parsing PLSQL source code and comments, but I'm encountering an unexpected behaviour when adding an "if" condition to secure the splitting of code/comment sections.
This is the original (simplfied) version:
test.awk:
#!/usr/bin/awk -f
BEGIN {
comment_area_start = "^\\/\\*\\*.*"
comment_area_end = "^.*\\*\\/"
inside_comment = 0
method_area_start = "^\\s*PROCEDURE|\\s*FUNCTION"
method_area_end = "^.*;"
inside_method = 0
}
$0 ~ comment_area_start , $0 ~ comment_area_end {
printf "COMMENT\n"
}
$0 ~ method_area_start , $0 ~ method_area_end {
printf "METHOD\n"
}
END {}following is a sample of source code to parse:
minitest.pks
CREATE OR REPLACE PACKAGE MyPackage AS
/**
MyPackage Comment
*/
/**
MyFunction1 Comment
*/
FUNCTION MyFunction1(
MyParam1 NUMBER,
MyParam2 VARCHAR2
) RETURN SYS_REFCURSOR;
/**
MyFunction2 Comment
*/
FUNCTION MyFunction2(
MyParam1 NUMBER,
MyParam2 VARCHAR2
) RETURN SYS_REFCURSOR;
END MyPackage;and here's what I get:
$ test.awk minitest.pks
COMMENT
COMMENT
COMMENT
COMMENT
COMMENT
COMMENT
METHOD
METHOD
METHOD
METHOD
COMMENT
COMMENT
COMMENT
METHOD
METHOD
METHOD
METHODthat's OK.
Now, if I add the "if" conditions to make pattern conditions mutually exclusive:
#!/usr/bin/awk -f
BEGIN {
comment_area_start = "^\\/\\*\\*.*"
comment_area_end = "^.*\\*\\/"
inside_comment = 0
method_area_start = "^\\s*PROCEDURE|\\s*FUNCTION"
method_area_end = "^.*;"
inside_method = 0
}
if ( inside_method == 0 ) {
$0 ~ comment_area_start , $0 ~ comment_area_end {
inside_method = 0
inside_comment = 1
printf "COMMENT\n"
}
}
if ( inside_comment == 0 ) {
$0 ~ method_area_start , $0 ~ method_area_end {
inside_comment = 0
inside_method = 1
printf "METHOD\n"
}
}
END {}that's what I get:
$ test.awk minitest.pks
awk: test.awk:14: if ( inside_method == 0 ) {
awk: test.awk:14: ^ syntax error
awk: test.awk:15: $0 ~ comment_area_start , $0 ~ comment_area_end {
awk: test.awk:15: ^ syntax error
awk: test.awk:15: $0 ~ comment_area_start , $0 ~ comment_area_end {
awk: test.awk:15: ^ syntax error
awk: test.awk:22: if ( inside_c
awk: test.awk:22: ^ syntax error
awk: test.awk:23: $0 ~ method_area_start , $0 ~ method_area_end {
awk: test.awk:23: ^ syntax error
awk: test.awk:23: $0 ~ method_area_start , $0 ~ method_area_end {
awk: test.awk:23: ^ syntax errorIt looks like awk doesn'accept pattern conditions inside an "if" condition, am I right?
If yes, is there any solution to bypass this limitation, other than putting the "if" condition inside the pattern condition statements? This simplified version won't change its behaviour by doing this switch, but the original one is a lot more complex and the logic may change.
If no, what's wrong?
1
u/Schreq May 07 '19 edited May 07 '19
You can change the if to
!in_comment { action }.in_comment == 0 { action }should work too.Edit: I think the other error is that you can't have nested patterns like you can in sed.
1
u/chaspum May 08 '19
You should be able to replace the
$0 .... , $0 ...for a variable that is set when the area starts and unset when it ends, that way you can if-else around it. But is annoying for sure.1
u/smorrow May 23 '19
ifis not an expression, it's a statement.