Allow //pragma inline or /*pragma inline*/ for inlining a function

They are transformed by the scanner to the identifier `inline`, which is how the parser identifies it. This solves the comment problem, but it results in a funny side effect. Now, in inline mode, /*pragma inline*/ will always be the identifier `inline`, therefore this is valid:

    integer /*pragma inline*/ = 5;
    llOwnerSay((string)inline); // will say 5

Not overly elegant, but better than making up a specific token or declaring comments as tokens or the like.
This commit is contained in:
Sei Lisa 2021-10-17 20:07:33 +02:00
parent 064a8c4319
commit bf72d2c3bf
2 changed files with 18 additions and 2 deletions

View file

@ -525,6 +525,18 @@ class parser(object):
if c == '/': if c == '/':
if self.script[self.pos:self.pos+1] == '/': if self.script[self.pos:self.pos+1] == '/':
self.pos += 1 self.pos += 1
if self.enable_inline and self.script.startswith(
'pragma inline', self.pos
) and not isalphanum_(self.script[self.pos + 13:
self.pos + 14]
):
self.pos += 12 # len('pragma inline') - 1
while self.script[self.pos] != '\n':
self.pos += 1
# Check for normal EOF. Note: 'inline' is not
# inserted if the file ends before a newline.
self.ceof()
return ('IDENT', 'inline')
self.ceof() self.ceof()
while self.script[self.pos] != '\n': while self.script[self.pos] != '\n':
self.pos += 1 self.pos += 1
@ -537,6 +549,10 @@ class parser(object):
elif self.script[self.pos:self.pos+1] == '*': elif self.script[self.pos:self.pos+1] == '*':
self.pos += 2 self.pos += 2
if self.enable_inline and self.script.startswith(
'pragma inline*/', self.pos-1):
self.pos += 14 # len('pragma inline*/') - 1
return ('IDENT', 'inline')
while self.script[self.pos-1:self.pos+1] != '*/': while self.script[self.pos-1:self.pos+1] != '*/':
self.pos += 1 self.pos += 1
self.ueof() # An unterminated multiline comment *is* unexpected EOF. self.ueof() # An unterminated multiline comment *is* unexpected EOF.

View file

@ -5,12 +5,12 @@ f1() inline
llOwnerSay("f1"); llOwnerSay("f1");
} }
f2(integer f2param) inline f2(integer f2param) /*pragma inline*/
{ {
llOwnerSay("f2:" + (string)f2param); llOwnerSay("f2:" + (string)f2param);
} }
vector f3(integer f3p1, string f3p2) inline vector f3(integer f3p1, string f3p2) //pragma inline
{ {
f2(f3p1); f2(f3p1);
integer f3p1; // test shading the parameter integer f3p1; // test shading the parameter