intellij-awk
intellij-awk copied to clipboard
Gawk built-in functions shadowing
Relates to https://github.com/xonixx/intellij-awk/issues/69
Understand what to do:
$ ./soft/gawk51 'function foo(gsub) { print gsub } BEGIN { foo(123) }'
gawk51: cmd. line:1: function foo(gsub) { print gsub } BEGIN { foo(123) }
gawk51: cmd. line:1: ^ syntax error
gawk51: cmd. line:1: function foo(gsub) { print gsub } BEGIN { foo(123) }
gawk51: cmd. line:1: ^ syntax error
$ ./soft/gawk51 'function foo(gensub) { print gensub } BEGIN { foo(123) }'
123
$ awk 'function foo(gensub) { print gensub } BEGIN { foo(123) }'
awk: cmd. line:1: function foo(gensub) { print gensub } BEGIN { foo(123) }
awk: cmd. line:1: ^ syntax error
awk: cmd. line:1: function foo(gensub) { print gensub } BEGIN { foo(123) }
awk: cmd. line:1: ^ syntax error
$ awk --version
GNU Awk 4.1.3, API: 1.1 (GNU MPFR 3.1.4, GNU MP 6.1.0)
$ ./soft/gawk51 --posix 'BEGIN { and = 123; print and }'
123
$ ./soft/gawk51 'BEGIN { and = 123; print and }'
gawk51: cmd. line:1: BEGIN { and = 123; print and }
gawk51: cmd. line:1: ^ syntax error
gawk51: cmd. line:1: BEGIN { and = 123; print and }
gawk51: cmd. line:1: ^ syntax error
$ ./soft/bwk 'BEGIN { and = 123; print and }'
123
$ ./soft/gawk51 'BEGIN { gensub = 123; print gensub }'
gawk51: cmd. line:1: BEGIN { gensub = 123; print gensub }
gawk51: cmd. line:1: ^ syntax error
gawk51: cmd. line:1: BEGIN { gensub = 123; print gensub }
gawk51: cmd. line:1: ^ syntax error
$ ./soft/gawk51 '@namespace "a" ; BEGIN { gensub = 123; print gensub }'
123
$ ./soft/gawk51 '@namespace "a" ; BEGIN { gsub = 123; print gsub }'
gawk51: cmd. line:1: @namespace "a" ; BEGIN { gsub = 123; print gsub }
gawk51: cmd. line:1: ^ syntax error
gawk51: cmd. line:1: @namespace "a" ; BEGIN { gsub = 123; print gsub }
gawk51: cmd. line:1: ^ syntax error
Parsing rules change depending on namespace:
$ ./soft/gawk51 'BEGIN { print mktime ("2021 01 01 01 01 01") }'
1609455661
$ ./soft/gawk51 '@namespace "a"; BEGIN { print mktime ("2021 01 01 01 01 01") }'
2021 01 01 01 01 01
$ ./soft/gawk51 '@namespace "awk"; BEGIN { print mktime ("2021 01 01 01 01 01") }'
1609455661
It's clear why this is done for Gawk - to not break the old scripts intended to work on Posix Awk. But how can we handle this better? In the naive approach we'll just fail to parse function f(and) {}
because and
is a Gawk bitwise function.
Relevant doc https://www.gnu.org/software/gawk/manual/html_node/Naming-Rules.html