Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions _scripts/templates/CSharp/st.Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static void SetupParse2(string input, string fn, bool quiet = false)
if (int.TryParse(part.Trim(), out int d))
ambig_decisions.Add(d);
}
else if (d_ambig_index >= 0 && (args[d_ambig_index].StartsWith("-ambig=")))
else if (d_ambig_index >= 0 && (args[d_ambig_index].StartsWith("--ambig=")))
{
ambig_decisions = new HashSet\<int>();
int prefix_len = 8;
Expand Down Expand Up @@ -102,7 +102,7 @@ public static void SetupParse2(string input, string fn, bool quiet = false)
var parser_startIndex = ai.startIndex;
var parser_stopIndex = ai.stopIndex;
var p = Parser.RuleNames.Select((value, index) => new { value, index })
.Where(pair => (pair.value == "prog"))
.Where(pair => (pair.value == "<start_symbol>"))
.Select(pair => pair.index).First();
var parser_startRuleIndex = p;
var parse_trees = ((MyParser)Parser).getAllPossibleParseTrees(
Expand Down Expand Up @@ -393,7 +393,6 @@ static void DoParse(ICharStream str, string input_name, int row_number)
if (token.Type == Antlr4.Runtime.TokenConstants.EOF)
break;
}
if (show_tokens) System.Console.Error.WriteLine(new_s.ToString());
total_count += i;
if (show_tokens) System.Console.Error.WriteLine(new_s.ToString());
lexer.Reset();
Expand Down
21 changes: 20 additions & 1 deletion csharp/v8-spec/Antlr4ng/CSharpParserBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,26 @@ export abstract class CSharpParserBase extends Parser {
return tok2 !== null && (tok2.type === CSharpLexer.Simple_Identifier || tok2.text === "_");
}

IsConstantPatternAhead(): boolean { return !this.IsDeclarationPatternAhead(); }
IsConstantPatternAhead(): boolean {
if (this.IsDeclarationPatternAhead()) return false;
const ts = this.tokenStream;
if (ts.LT(1)?.type === CSharpLexer.TK_LPAREN) {
let depth = 0, i = 1;
while (true) {
const tok = ts.LT(i++);
if (!tok || tok.type < 0) break;
if (tok.type === CSharpLexer.TK_LPAREN) depth++;
else if (tok.type === CSharpLexer.TK_RPAREN) { depth--; if (depth === 0) break; }
else if (tok.type === CSharpLexer.TK_COMMA && depth === 1) return false;
}
}
// Identifier followed immediately by '(' → type-headed positional pattern.
if (ts.LT(1)?.type === CSharpLexer.Simple_Identifier
&& ts.LT(2)?.type === CSharpLexer.TK_LPAREN) return false;
return true;
}

IsPositionalPatternAhead(): boolean { return !this.IsConstantPatternAhead(); }

IsImplicitlyTypedLocalVariable(): boolean {
const tok = this.tokenStream.LT(1);
Expand Down
49 changes: 48 additions & 1 deletion csharp/v8-spec/CSharp/CSharpParserBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,54 @@ public bool IsDeclarationPatternAhead()
}
}

public bool IsConstantPatternAhead() => !IsDeclarationPatternAhead();
public bool IsConstantPatternAhead()
{
if (IsDeclarationPatternAhead()) return false;

// A '(' followed by a comma at depth 1 is a tuple-positional pattern
// like ("rock", "scissors") or (0, 0). Tuple expressions are not C#
// compile-time constants, so this can only match positional_pattern
// (alt 4), not constant_pattern (alt 2). Return false to suppress
// alt 2 and eliminate the decision-31 ambiguity.
var ts = (ITokenStream)InputStream;
if (ts.LT(1).Type == CSharpLexer.TK_LPAREN)
{
int depth = 0, i = 1;
while (true)
{
IToken tok = ts.LT(i++);
if (tok.Type == TokenConstants.EOF) break;
if (tok.Type == CSharpLexer.TK_LPAREN) depth++;
else if (tok.Type == CSharpLexer.TK_RPAREN) { depth--; if (depth == 0) break; }
else if (tok.Type == CSharpLexer.TK_COMMA && depth == 1) return false;
}
}

// An identifier followed by '(' after a type_ is a type-headed positional
// pattern like Point(0, 0) or Shape.Circle(x, y). Invocation expressions
// cannot be compile-time constants, so this cannot be a constant_pattern.
IToken t1 = ts.LT(1);
if (t1 != null && t1.Type == CSharpLexer.Simple_Identifier)
{
var par = new CSharpParser((ITokenStream)InputStream);
par.RemoveErrorListeners();
par.ErrorHandler = new BailErrorStrategy();
int savedIndex = InputStream.Index;
try
{
par.type_();
IToken next = ((CommonTokenStream)InputStream).LT(1);
if (next != null && next.Type == CSharpLexer.TK_LPAREN)
return false; // type_ '(' → positional_pattern
}
catch { }
finally { InputStream.Seek(savedIndex); }
}

return true;
}

public bool IsPositionalPatternAhead() => !IsConstantPatternAhead();

Comment thread
kaby76 marked this conversation as resolved.
//--------------------------------------------------------------------------------------
// non_nullable_reference_type disambiguation — decision 15
Expand Down
44 changes: 33 additions & 11 deletions csharp/v8-spec/CSharpParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ pattern
: {this.IsDeclarationPatternAhead()}? declaration_pattern
| {this.IsConstantPatternAhead()}? constant_pattern
| var_pattern
| positional_pattern
| {this.IsPositionalPatternAhead()}? positional_pattern
| property_pattern
| discard_pattern
;
Expand Down Expand Up @@ -868,9 +868,14 @@ await_expression
;

// Source: §12.10 Range operator
// Original:
//range_expression
// : unary_expression
// | unary_expression? '..' unary_expression?
// ;
range_expression
: unary_expression
| unary_expression? '..' unary_expression?
: unary_expression ('..' unary_expression?)?
| '..' unary_expression?
;

// Source: §12.11 Switch expression
Expand Down Expand Up @@ -958,9 +963,14 @@ conditional_or_expression
;

// Source: §12.17 The null coalescing operator
// Original:
//null_coalescing_expression
// : conditional_or_expression
// | conditional_or_expression '??' null_coalescing_expression
// | throw_expression
// ;
null_coalescing_expression
: conditional_or_expression
| conditional_or_expression '??' null_coalescing_expression
: conditional_or_expression ('??' null_coalescing_expression)?
| throw_expression
;

Expand All @@ -983,11 +993,19 @@ declaration_expression
// ;

// Source: §12.20 Conditional operator
// Original rule before left-factoring:
//conditional_expression
// : null_coalescing_expression
// | null_coalescing_expression '?' expression ':' expression
// | null_coalescing_expression '?' 'ref' variable_reference ':' 'ref' variable_reference
// ;
conditional_expression
: null_coalescing_expression
| null_coalescing_expression '?' expression ':' expression
| null_coalescing_expression '?' 'ref' variable_reference ':'
'ref' variable_reference
: null_coalescing_expression (
'?' (
expression ':' expression
| 'ref' variable_reference ':' 'ref' variable_reference
)
)?
;

// Source: §12.21.1 General
Expand Down Expand Up @@ -1130,9 +1148,13 @@ assignment_operator
;

// Source: §12.24 Expression
// Original:
//expression
// : non_assignment_expression
// | assignment
// ;
expression
: non_assignment_expression
| assignment
: non_assignment_expression (assignment_operator expression)?
;

non_assignment_expression
Expand Down
45 changes: 44 additions & 1 deletion csharp/v8-spec/Cpp/CSharpParserBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,52 @@ bool CSharpParserBase::IsDeclarationPatternAhead()

bool CSharpParserBase::IsConstantPatternAhead()
{
return !IsDeclarationPatternAhead();
if (IsDeclarationPatternAhead()) return false;
antlr4::Token *first = _input->LT(1);
if (first && static_cast<int>(first->getType()) == CSharpLexer::TK_LPAREN)
{
int depth = 0, i = 1;
while (true)
{
antlr4::Token *tok = _input->LT(i++);
if (!tok) break;
int tt = static_cast<int>(tok->getType());
if (tt < 0) break; // EOF
if (tt == CSharpLexer::TK_LPAREN) depth++;
else if (tt == CSharpLexer::TK_RPAREN) { depth--; if (depth == 0) break; }
else if (tt == CSharpLexer::TK_COMMA && depth == 1) return false;
}
}
// Type-headed positional pattern: speculative parse of type_() followed by '('
// e.g. Point(0, 0) — LT(1) is an identifier, not '(', so the tuple scan above
// was skipped. A successful type_() parse whose next token is '(' means this
// is a positional_pattern, not a constant_pattern.
if (first && static_cast<int>(first->getType()) == CSharpLexer::Simple_Identifier)
{
size_t savedIndex = _input->index();
auto *par = new CSharpParser(_input);
par->removeErrorListeners();
par->setErrorHandler(std::make_shared<antlr4::BailErrorStrategy>());
try
{
par->type_();
antlr4::Token *next = _input->LT(1);
if (next && static_cast<int>(next->getType()) == CSharpLexer::TK_LPAREN)
{
_input->seek(savedIndex);
delete par;
return false;
}
}
catch (...) { }
_input->seek(savedIndex);
delete par;
}
return true;
}

bool CSharpParserBase::IsPositionalPatternAhead() { return !IsConstantPatternAhead(); }

bool CSharpParserBase::IsTypeParameterName()
{
antlr4::Token *t = _input->LT(1);
Expand Down
1 change: 1 addition & 0 deletions csharp/v8-spec/Cpp/CSharpParserBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class CSharpParserBase : public antlr4::Parser
bool IsCastExpressionAhead();
bool IsDeclarationPatternAhead();
bool IsConstantPatternAhead();
bool IsPositionalPatternAhead();
bool IsTypeParameterName();
bool IsValueTypeName();
bool IsReferenceTypeName();
Expand Down
30 changes: 29 additions & 1 deletion csharp/v8-spec/Dart/CSharpParserBase.dart
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,35 @@ abstract class CSharpParserBase extends Parser {
(tok2.type == CSharpLexer.TOKEN_Simple_Identifier || tok2.text == '_');
}

bool IsConstantPatternAhead() => !IsDeclarationPatternAhead();
bool IsConstantPatternAhead() {
if (IsDeclarationPatternAhead()) return false;
final ts = inputStream as TokenStream;
final first = ts.LT(1);
if (first != null && first.type == CSharpLexer.TOKEN_TK_LPAREN) {
int depth = 0, i = 1;
while (true) {
final tok = ts.LT(i++);
if (tok == null || tok.type < 0) break;
if (tok.type == CSharpLexer.TOKEN_TK_LPAREN) {
depth++;
} else if (tok.type == CSharpLexer.TOKEN_TK_RPAREN) {
depth--;
if (depth == 0) break;
} else if (tok.type == CSharpLexer.TOKEN_TK_COMMA && depth == 1) {
return false;
}
}
}
// Identifier followed immediately by '(' → type-headed positional pattern.
final second = ts.LT(2);
if (first != null && first.type == CSharpLexer.TOKEN_Simple_Identifier &&
second != null && second.type == CSharpLexer.TOKEN_TK_LPAREN) {
return false;
}
return true;
}

bool IsPositionalPatternAhead() => !IsConstantPatternAhead();

bool IsImplicitlyTypedLocalVariable() {
final tok = (inputStream as TokenStream).LT(1);
Expand Down
42 changes: 40 additions & 2 deletions csharp/v8-spec/Go/CSharpParserBase.go
Original file line number Diff line number Diff line change
Expand Up @@ -801,8 +801,46 @@ func (p *CSharpParserBase) IsDeclarationPatternAhead() bool {
return tok2 != nil && (tok2.GetTokenType() == CSharpLexerSimple_Identifier || tok2.GetText() == "_")
}

// IsConstantPatternAhead is the complement of IsDeclarationPatternAhead.
func (p *CSharpParserBase) IsConstantPatternAhead() bool { return !p.IsDeclarationPatternAhead() }
// IsConstantPatternAhead returns false for declaration patterns and for paren-with-comma
// (tuple-positional) patterns, routing the latter exclusively to positional_pattern.
func (p *CSharpParserBase) IsConstantPatternAhead() bool {
if p.IsDeclarationPatternAhead() {
return false
}
ts := p.GetTokenStream()
tok1 := ts.LT(1)
if tok1 != nil && tok1.GetTokenType() == CSharpLexerTK_LPAREN {
depth, i := 0, 1
for {
tok := ts.LT(i)
i++
if tok == nil || tok.GetTokenType() == antlr.TokenEOF {
break
}
tt := tok.GetTokenType()
if tt == CSharpLexerTK_LPAREN {
depth++
} else if tt == CSharpLexerTK_RPAREN {
depth--
if depth == 0 {
break
}
} else if tt == CSharpLexerTK_COMMA && depth == 1 {
return false
}
}
}
// Identifier followed immediately by '(' → type-headed positional pattern.
tok2 := ts.LT(2)
if tok1 != nil && tok1.GetTokenType() == CSharpLexerSimple_Identifier &&
tok2 != nil && tok2.GetTokenType() == CSharpLexerTK_LPAREN {
return false
}
return true
}

// IsPositionalPatternAhead is the complement of IsConstantPatternAhead.
func (p *CSharpParserBase) IsPositionalPatternAhead() bool { return !p.IsConstantPatternAhead() }

// IsImplicitlyTypedLocalVariable returns true when LT(1)='var' and context implies implicit typing.
func (p *CSharpParserBase) IsImplicitlyTypedLocalVariable() bool {
Expand Down
38 changes: 37 additions & 1 deletion csharp/v8-spec/Java/CSharpParserBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,43 @@ public boolean IsDeclarationPatternAhead()
finally { _input.seek(savedIndex); }
}

public boolean IsConstantPatternAhead() { return !IsDeclarationPatternAhead(); }
public boolean IsConstantPatternAhead()
{
if (IsDeclarationPatternAhead()) return false;
Token t1 = ((CommonTokenStream)_input).LT(1);
if (t1 != null && t1.getType() == CSharpLexer.TK_LPAREN)
{
int depth = 0, i = 1;
while (true)
{
Token tok = ((CommonTokenStream)_input).LT(i++);
if (tok == null || tok.getType() == Token.EOF) break;
int tt = tok.getType();
if (tt == CSharpLexer.TK_LPAREN) depth++;
else if (tt == CSharpLexer.TK_RPAREN) { depth--; if (depth == 0) break; }
else if (tt == CSharpLexer.TK_COMMA && depth == 1) return false;
}
}
// Identifier followed by '(' after type_ → type-headed positional pattern.
if (t1 != null && t1.getType() == CSharpLexer.Simple_Identifier)
{
int savedIndex = _input.index();
CSharpParser par = new CSharpParser(_input);
par.removeErrorListeners();
par.setErrorHandler(new BailErrorStrategy());
try
{
par.type_();
Token next = ((CommonTokenStream)_input).LT(1);
if (next != null && next.getType() == CSharpLexer.TK_LPAREN) return false;
}
catch (Exception e) { }
finally { _input.seek(savedIndex); }
}
return true;
}

public boolean IsPositionalPatternAhead() { return !IsConstantPatternAhead(); }

public boolean IsImplicitlyTypedLocalVariable()
{
Expand Down
Loading
Loading