Skip to content

Commit

Permalink
Fix for #4312 (#4313)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaby76 authored Nov 9, 2024
1 parent 5ed2a3a commit 00ad7b0
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 17 deletions.
34 changes: 34 additions & 0 deletions bison/Antlr4ng/BisonLexerBase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { CommonToken, Lexer, CharStream, Token } from "antlr4ng";
import { BisonLexer } from './BisonLexer.js';

export default abstract class BisonLexerBase extends Lexer {
percent_percent_count: number;

constructor(input: CharStream) {
super(input);
this.percent_percent_count = 0;
}

reset() {
this.percent_percent_count = 0;
super.reset();
}

NextMode()
{
++this.percent_percent_count;
if (this.percent_percent_count == 1)
{
return;
} else if (this.percent_percent_count == 2)
{
this.pushMode(BisonLexer.EpilogueMode);
return;
} else
{
this.type = BisonLexer.PercentPercent;
return;
}
}
}

31 changes: 31 additions & 0 deletions bison/Antlr4ng/transformGrammar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""The script transforms the grammar to fit for the c++ target """
import sys
import re
import shutil
from glob import glob
from pathlib import Path

def main():
"""Executes the script."""
for file in glob("./*.g4"):
transform_grammar(file)

def transform_grammar(file_path):
"""Transforms the grammar to fit for the target"""
print("Altering " + file_path)
if not Path(file_path).is_file:
print(f"Could not find file: {file_path}")
sys.exit(1)

shutil.move(file_path, file_path + ".bak")
with open(file_path + ".bak",'r', encoding="utf-8") as input_file:
with open(file_path, 'w', encoding="utf-8") as output_file:
for line in input_file:
line = re.sub(r"(\/\/ Insert here @header for C\+\+ lexer\.)",\
'@header {import BisonLexerBase from "./BisonLexerBase.js"}', line)
output_file.write(line)

print("Writing ...")

if __name__ == '__main__':
main()
9 changes: 5 additions & 4 deletions bison/BisonLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
// Copyright 2020-2022
// MIT License

// $antlr-format alignTrailingComments true, columnLimit 150, maxEmptyLinesToKeep 1, reflowComments false, useTab false
// $antlr-format allowShortRulesOnASingleLine true, allowShortBlocksOnASingleLine true, minEmptyLines 0, alignSemicolons ownLine
// $antlr-format alignColons trailing, singleLineOverrulesHangingColon true, alignLexerCommands true, alignLabels true, alignTrailers true

// $antlr-format alignColons trailing, alignLabels true, alignLexerCommands true, alignSemicolons ownLine, alignTrailers true
// $antlr-format alignTrailingComments true, allowShortBlocksOnASingleLine true, allowShortRulesOnASingleLine true, columnLimit 150
// $antlr-format maxEmptyLinesToKeep 1, minEmptyLines 0, reflowComments false, singleLineOverrulesHangingColon true, useTab false

lexer grammar BisonLexer;

Expand Down Expand Up @@ -141,7 +142,7 @@ PercentPercent: '%%' { this.NextMode(); };
option name, with a single string argument. Otherwise, add exceptions
to ../build-aux/cross-options.pl. */
NONASSOC: '%binary';
PERCENT_BINARY: '%binary';
CODE: '%code';
Expand Down
20 changes: 8 additions & 12 deletions bison/BisonParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
| Grammar. |
\==========*/

// $antlr-format alignTrailingComments true, columnLimit 150, minEmptyLines 1, maxEmptyLinesToKeep 1, reflowComments false, useTab false
// $antlr-format allowShortRulesOnASingleLine false, allowShortBlocksOnASingleLine true, alignSemicolons hanging, alignColons hanging

// $antlr-format alignColons hanging, alignSemicolons hanging, alignTrailingComments true, allowShortBlocksOnASingleLine true
// $antlr-format allowShortRulesOnASingleLine false, columnLimit 150, maxEmptyLinesToKeep 1, minEmptyLines 1, reflowComments false, useTab false

parser grammar BisonParser;

Expand Down Expand Up @@ -74,8 +75,7 @@ prologue_declaration
;

params
: params actionBlock
| actionBlock
: actionBlock+
;

/*----------------------.
Expand Down Expand Up @@ -118,6 +118,7 @@ precedence_declarator
: PERCENT_LEFT
| PERCENT_RIGHT
| PERCENT_NONASSOC
| PERCENT_BINARY
| PRECEDENCE
;

Expand Down Expand Up @@ -193,9 +194,7 @@ alias
// FOO and 'foo' as two different symbols instead of aliasing them.

token_decls_for_prec
: token_decl_for_prec+
| TAG token_decl_for_prec+
| token_decls_for_prec TAG token_decl_for_prec+
: (token_decl_for_prec+ | TAG token_decl_for_prec+) (TAG token_decl_for_prec+)*
;

// One token declaration for precedence declaration.
Expand All @@ -212,18 +211,15 @@ token_decl_for_prec
// A non empty list of typed symbols (for %type).

symbol_decls
: symbol+
| TAG symbol+
| symbol_decls TAG symbol+
: (symbol+ | TAG symbol+) (TAG symbol+)*
;

/*------------------------------------------.
| The grammar section: between the two %%. |
`------------------------------------------*/

bison_grammar
: rules_or_grammar_declaration
| bison_grammar rules_or_grammar_declaration
: rules_or_grammar_declaration+
;

/* As a Bison extension, one can use the grammar declarations in the
Expand Down
2 changes: 1 addition & 1 deletion bison/desc.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8" ?>
<desc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../_scripts/desc.xsd">
<targets>Cpp;CSharp;Dart;Go;Java;JavaScript;Python3</targets>
<targets>Antlr4ng;Cpp;CSharp;Dart;Go;Java;JavaScript;Python3;TypeScript</targets>
</desc>

0 comments on commit 00ad7b0

Please sign in to comment.