Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed the SHEBANG detection #4290

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
5 changes: 5 additions & 0 deletions rust/CSharp/RustLexerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public bool next(char expect){
return _input.LA(1) == expect;
}


public bool nexti(int expect){
return _input.LA(1) == expect;
}

public bool floatDotPossible(){
int next = _input.LA(1);
// only block . _ identifier after float
Expand Down
33 changes: 33 additions & 0 deletions rust/CSharp/transformGrammar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import sys, os, re, shutil
from glob import glob
from pathlib import Path

def main(argv):
for file in glob("*.g4"):
fix(file)

def fix(file_path):
print("Altering " + file_path)
if not os.path.exists(file_path):
print(f"Could not find file: {file_path}")
sys.exit(1)
parts = os.path.split(file_path)
file_name = parts[-1]

shutil.move(file_path, file_path + ".bak")
input_file = open(file_path + ".bak",'r')
output_file = open(file_path, 'w')
for x in input_file:
if 'this._input' in x:
x = x.replace("this._input", "this.InputStream")


output_file.write(x)
output_file.flush()

print("Writing ...")
input_file.close()
output_file.close()

if __name__ == '__main__':
main(sys.argv)
3 changes: 3 additions & 0 deletions rust/Cpp/RustLexerBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class RustLexerBase : public antlr4::Lexer {
bool next(char expect){
return _input->LA(1) == expect;
}
bool nexti(int expect){
return _input->LA(1) == expect;
}

bool floatDotPossible(){
size_t next = _input->LA(1);
Expand Down
6 changes: 5 additions & 1 deletion rust/Java/RustLexerBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public boolean next(char expect){
return _input.LA(1) == expect;
}

public boolean nexti(int expect){
return _input.LA(1) == expect;
}

public boolean floatDotPossible(){
int next = _input.LA(1);
// only block . _ identifier after float
Expand Down Expand Up @@ -82,4 +86,4 @@ public boolean floatLiteralPossible(){
return true;
}
}
}
}
61 changes: 35 additions & 26 deletions rust/Python3/RustLexerBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
from antlr4 import *
from antlr4.InputStream import InputStream


class RustLexerBase(Lexer):
flt_mp = set()
RustLexer = None

def __init__(self, input: InputStream, output: TextIO = ...) -> None:
super().__init__(input, output)
self.token_lookbehind: tuple[Optional[Token], Optional[Token]] = (None, None)
Expand All @@ -14,32 +16,33 @@ def __init__(self, input: InputStream, output: TextIO = ...) -> None:
from RustLexer import RustLexer
RustLexerBase.RustLexer = RustLexer
RustLexerBase.flt_mp = {
RustLexer.STRING_LITERAL,
RustLexer.RAW_STRING_LITERAL,
RustLexer.BYTE_LITERAL,
RustLexer.BYTE_STRING_LITERAL,
RustLexer.RAW_BYTE_STRING_LITERAL,
RustLexer.INTEGER_LITERAL,
RustLexer.DEC_LITERAL,
RustLexer.HEX_LITERAL,
RustLexer.OCT_LITERAL,
RustLexer.BIN_LITERAL,
RustLexer.KW_SUPER,
RustLexer.KW_SELFVALUE,
RustLexer.KW_SELFTYPE,
RustLexer.KW_CRATE,
RustLexer.KW_DOLLARCRATE,
RustLexer.RCURLYBRACE,
RustLexer.RSQUAREBRACKET,
RustLexer.RPAREN,
RustLexer.KW_AWAIT,
RustLexer.NON_KEYWORD_IDENTIFIER,
RustLexer.RAW_IDENTIFIER,
RustLexer.KW_MACRORULES,
RustLexer.GT
}
RustLexer.STRING_LITERAL,
RustLexer.RAW_STRING_LITERAL,
RustLexer.BYTE_LITERAL,
RustLexer.BYTE_STRING_LITERAL,
RustLexer.RAW_BYTE_STRING_LITERAL,
RustLexer.INTEGER_LITERAL,
RustLexer.DEC_LITERAL,
RustLexer.HEX_LITERAL,
RustLexer.OCT_LITERAL,
RustLexer.BIN_LITERAL,
RustLexer.KW_SUPER,
RustLexer.KW_SELFVALUE,
RustLexer.KW_SELFTYPE,
RustLexer.KW_CRATE,
RustLexer.KW_DOLLARCRATE,
RustLexer.RCURLYBRACE,
RustLexer.RSQUAREBRACKET,
RustLexer.RPAREN,
RustLexer.KW_AWAIT,
RustLexer.NON_KEYWORD_IDENTIFIER,
RustLexer.RAW_IDENTIFIER,
RustLexer.KW_MACRORULES,
RustLexer.GT,
}

"""LOOK BEHIND TOKENS"""

def nextToken(self):
next: Token = super().nextToken()

Expand All @@ -57,6 +60,12 @@ def next(self, expect) -> bool:
else:
return self._input.LA(1) == expect

def nexti(self, expect) -> bool:
if isinstance(expect, str):
return chr(self._input.LA(1)) == expect
else:
return self._input.LA(1) == expect

def floatDotPossible(self):
next = chr(self._input.LA(1))
# print(f'INFO: floatpossible ? {next} = {chr(next)}')
Expand All @@ -81,10 +90,10 @@ def floatDotPossible(self):

def floatLiteralPossible(self):
prev, current = self.token_lookbehind

if prev == None or current == None:
return True
elif current.type != RustLexerBase.RustLexer.DOT:
return True
else:
return prev.type not in RustLexerBase.flt_mp
return prev.type not in RustLexerBase.flt_mp
4 changes: 3 additions & 1 deletion rust/RustLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ OUTER_BLOCK_DOC:

BLOCK_COMMENT_OR_DOC: ( BLOCK_COMMENT | INNER_BLOCK_DOC | OUTER_BLOCK_DOC) -> channel (HIDDEN);

SHEBANG: {this.SOF()}? '\ufeff'? '#!' ~[\r\n]* -> channel(HIDDEN);
SHEBANG: {this.SOF()}? '\ufeff'? '#!' {this.nexti(97)}? ~[\r\n]* -> channel(HIDDEN);
// Shebang not in first line and #! cannot be followed by a '[' character shebang


//ISOLATED_CR
// : '\r' {_input.LA(1)!='\n'}// not followed with \n ;
Expand Down
6 changes: 5 additions & 1 deletion rust/TypeScript/RustLexerBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export default abstract class RustLexerBase extends Lexer {
next(expect: string): boolean {
return this._input.LA(1) === expect.charCodeAt(0);
}
nexti(expect: number): boolean {
return this._input.LA(1) === expect;
}


// Determine if a float dot is possible based on the next character
floatDotPossible(): boolean {
Expand Down Expand Up @@ -88,4 +92,4 @@ export default abstract class RustLexerBase extends Lexer {
return true;
}
}
}
}
Loading