Skip to content

Commit

Permalink
[javascript] problem with continue statement and newline followed by …
Browse files Browse the repository at this point in the history
…whitespace (antlr#4046)

* [javascript] problem with continue statement and newline followed by
whitespace OR comment antlr#3772

* remove the here method because is not used anymore;

* fix for the go base parser, lineTerminatorAhead should return false if
the token is not on the hidden channel
  • Loading branch information
meraedit authored Apr 15, 2024
1 parent dcfbe51 commit a8ba792
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 147 deletions.
26 changes: 1 addition & 25 deletions javascript/javascript/CSharp/JavaScriptParserBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected bool next(string str)

protected bool notLineTerminator()
{
return !here(LineTerminator);
return !lineTerminatorAhead();
}

protected bool notOpenBraceAndNotFunction()
Expand All @@ -63,30 +63,6 @@ protected bool closeBrace()
return ((ITokenStream)this.InputStream).LT(1).Type == CloseBrace;
}

/// <summary>Returns true if on the current index of the parser's
/// token stream a token of the given type exists on the
/// Hidden channel.
/// </summary>
/// <param name="type">
/// The type of the token on the Hidden channel to check.
/// </param>
protected bool here(int type)
{
// Get the most recently emitted token.
IToken currentToken = ((ITokenStream)this.InputStream).LT(-1);

// Get the next token index.
int nextTokenIndex = currentToken == null ? 0 : currentToken.TokenIndex + 1;

// Get the token after the `currentToken`. By using `_input.get(index)`,
// we also grab a token that is (possibly) on the HIDDEN channel.
IToken nextToken = ((ITokenStream)this.InputStream).Get(nextTokenIndex);

// Check if the token resides on the HIDDEN channel and if it's of the
// provided type.
return (nextToken.Channel == Lexer.Hidden) && (nextToken.Type == type);
}

/// <summary>
/// Returns true if on the current index of the parser's
/// token stream a token exists on the Hidden channel which
Expand Down
19 changes: 1 addition & 18 deletions javascript/javascript/Cpp/JavaScriptParserBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ bool JavaScriptParserBase::next(std::string str)

bool JavaScriptParserBase::notLineTerminator()
{
return !here(JavaScriptParser::LineTerminator);
return !lineTerminatorAhead();
}

bool JavaScriptParserBase::notOpenBraceAndNotFunction()
Expand All @@ -39,23 +39,6 @@ bool JavaScriptParserBase::closeBrace()
return _input->LT(1)->getType() == JavaScriptParser::CloseBrace;
}

bool JavaScriptParserBase::here(int type)
{
// Get the most recently emitted token.
auto currentToken = _input->LT(-1);

// Get the next token index.
int nextTokenIndex = currentToken == nullptr ? 0 : currentToken->getTokenIndex() + 1;

// Get the token after the `currentToken`. By using `_input.get(index)`,
// we also grab a token that is (possibly) on the HIDDEN channel.
auto nextToken = _input->get(nextTokenIndex);

// Check if the token resides on the HIDDEN channel and if it's of the
// provided type.
return (nextToken->getChannel() == Lexer::HIDDEN) && (nextToken->getType() == type);
}

bool JavaScriptParserBase::lineTerminatorAhead()
{
// Get the token ahead of the current index.
Expand Down
1 change: 0 additions & 1 deletion javascript/javascript/Cpp/JavaScriptParserBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ class JavaScriptParserBase : public antlr4::Parser {
bool notLineTerminator();
bool notOpenBraceAndNotFunction();
bool closeBrace();
bool here(int type);
bool lineTerminatorAhead();
};
27 changes: 2 additions & 25 deletions javascript/javascript/Go/javascript_parser_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (p *JavaScriptParserBase) next(str string) bool {
}

func (p *JavaScriptParserBase) notLineTerminator() bool {
return !p.here(JavaScriptParserLineTerminator)
return !p.lineTerminatorAhead()
}

func (p *JavaScriptParserBase) notOpenBraceAndNotFunction() bool {
Expand All @@ -44,29 +44,6 @@ func (p *JavaScriptParserBase) closeBrace() bool {
return p.GetTokenStream().LT(1).GetTokenType() == JavaScriptParserCloseBrace
}

// Returns true if on the current index of the parser's
// token stream a token of the given type exists on the
// Hidden channel.
func (p *JavaScriptParserBase) here(_type int) bool {
// Get the most recently emitted token.
currentToken := p.GetTokenStream().LT(-1)

// Get the next token index.
nextTokenIndex := 0

if currentToken != nil {
nextTokenIndex = currentToken.GetTokenIndex() + 1
}

// Get the token after the `currentToken`. By using `_input.get(index)`,
// we also grab a token that is (possibly) on the HIDDEN channel.
nextToken := p.GetTokenStream().Get(nextTokenIndex);

// Check if the token resides on the HIDDEN channel and if it's of the
// provided type.
return nextToken.GetChannel() == antlr.LexerHidden && nextToken.GetTokenType() == _type
}

// Returns true if on the current index of the parser's
// token stream a token exists on the Hidden channel which
// either is a line terminator, or is a multi line comment that
Expand All @@ -81,7 +58,7 @@ func (p *JavaScriptParserBase) lineTerminatorAhead() bool {

if ahead.GetChannel() != antlr.LexerHidden {
// We're only interested in tokens on the HIDDEN channel.
return true
return false
}

if ahead.GetTokenType() == JavaScriptParserLineTerminator {
Expand Down
32 changes: 1 addition & 31 deletions javascript/javascript/Java/JavaScriptParserBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected boolean next(String str) {
}

protected boolean notLineTerminator() {
return !here(JavaScriptParser.LineTerminator);
return !lineTerminatorAhead();
}

protected boolean notOpenBraceAndNotFunction() {
Expand All @@ -51,36 +51,6 @@ protected boolean closeBrace() {
return _input.LT(1).getType() == JavaScriptParser.CloseBrace;
}

/**
* Returns {@code true} iff on the current index of the parser's
* token stream a token of the given {@code type} exists on the
* {@code HIDDEN} channel.
*
* @param type
* the type of the token on the {@code HIDDEN} channel
* to check.
*
* @return {@code true} iff on the current index of the parser's
* token stream a token of the given {@code type} exists on the
* {@code HIDDEN} channel.
*/
private boolean here(final int type) {

// Get the most recently emitted token.
Token currentToken = _input.LT(-1);

// Get the next token index.
int nextTokenIndex = currentToken == null ? 0 : currentToken.getTokenIndex() + 1;

// Get the token after the `currentToken`. By using `_input.get(index)`,
// we also grab a token that is (possibly) on the HIDDEN channel.
Token nextToken = _input.get(nextTokenIndex);

// Check if the token resides on the HIDDEN channel and if it's of the
// provided type.
return (nextToken.getChannel() == Lexer.HIDDEN) && (nextToken.getType() == type);
}

/**
* Returns {@code true} iff on the current index of the parser's
* token stream a token exists on the {@code HIDDEN} channel which
Expand Down
18 changes: 1 addition & 17 deletions javascript/javascript/JavaScript/JavaScriptParserBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default class JavaScriptParserBase extends antlr4.Parser {
}

notLineTerminator() {
return !this.here(JavaScriptParser.LineTerminator);
return !this.lineTerminatorAhead();
}

notOpenBraceAndNotFunction() {
Expand All @@ -43,22 +43,6 @@ export default class JavaScriptParserBase extends antlr4.Parser {
return this._input.LT(1).type === JavaScriptParser.CloseBrace;
}

here(type) {
// Get the most recently emitted token.
const currentToken = this._input.LT(-1);

// Get the next token index.
const nextTokenIndex = currentToken == null ? 0 : currentToken.tokenIndex + 1;

// Get the token after the `currentToken`. By using `_input.get(index)`,
// we also grab a token that is (possibly) on the HIDDEN channel.
const nextToken = this._input.get(nextTokenIndex);

// Check if the token resides on the HIDDEN channel and if it's of the
// provided type.
return nextToken.channel === antlr4.Lexer.HIDDEN && nextToken.type === type;
}

lineTerminatorAhead() {
let possibleIndexEosToken = this.getCurrentToken().tokenIndex - 1;
if (possibleIndexEosToken < 0) return false;
Expand Down
31 changes: 1 addition & 30 deletions javascript/javascript/Python3/JavaScriptParserBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def next(self, s: str) -> bool:
def notLineTerminator(self) -> bool:
JavaScriptParser = self.parser()

return not self.here(JavaScriptParser.LineTerminator)
return not self.lineTerminatorAhead()

def notOpenBraceAndNotFunction(self) -> bool:
JavaScriptParser = self.parser()
Expand All @@ -42,35 +42,6 @@ def closeBrace(self) -> bool:

return self._input.LT(1).type == JavaScriptParser.CloseBrace

def here(self, tokenType: int) -> bool:
"""
Returns {@code true} iff on the current index of the parser's
token stream a token of the given {@code type} exists on the
{@code HIDDEN} channel.
:param:type:
the type of the token on the {@code HIDDEN} channel
to check.
:return:{@code true} iff on the current index of the parser's
token stream a token of the given {@code type} exists on the
{@code HIDDEN} channel.
"""
# Get the token ahead of the current index.
assert isinstance(self.getCurrentToken(), Token)

# Get the most recently emitted token.
currentToken: Token = self._input.LT(-1)

# Get the next token index.
nextTokenIndex = 0 if currentToken is None else currentToken.tokenIndex + 1

# Get the token after the `currentToken`. By using `_input.get(index)`,
# we also grab a token that is (possibly) on the HIDDEN channel.
nextToken: Token = self._input.get(nextTokenIndex)

# Check if the token resides on the HIDDEN channel and if it's of the
# provided type.
return (nextToken.channel == Lexer.HIDDEN) and (nextToken.type == tokenType)

def lineTerminatorAhead(self) -> bool:
"""
Returns {@code true} iff on the current index of the parser's
Expand Down
24 changes: 24 additions & 0 deletions javascript/javascript/examples/Continue.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,28 @@ for (var i = 0; i < [].length; i++) {
var o = {}
if (false) continue
o[test]

if (false) continue// comment
o[test]

if (false) continue // comment
o[test]

if (false) continue // comment

o[test]

if (false) continue/*
* multiline comment
*/
o[test]

if (false) continue /*
* multiline comment
*/
o[test]
}

mylabel: for (var i = 0; i < [].length; i++) {
if (true) continue mylabel;
}

0 comments on commit a8ba792

Please sign in to comment.