Skip to content

Commit 919924e

Browse files
authored
[gvpr] Add new grammar gvpr (fix for #3803) (#3809)
* Fix for #3803 * Add tests from graphviz source code. Reformat grammar. * Add testing. * Rename to avoid symbol conflict. * Update grammar, targets. * Remove ambiguity. * Update statement_list and action_list to not be empty. * Update grammar with prec/assoc, types, semicolon decls. * Split grammar because semantic predicate needed. Added tests. * Update errors file. * Adding Java target. * Fix java target. * Add Dart target. * Add JavaScript target. * Add TypeScript port. * Add Python3 port. * Add Cpp port. * Add Go port. * Add new tests from graphviz source code (renamed with .gvpr extension). Fix grammar a little. * Fix parse errors in new tests. * Move DigitSquence down in grammar, and change to fragment (it should not be a token). * Add test, update escape char set.
1 parent 78aa006 commit 919924e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+2994
-0
lines changed

gvpr/CSharp/GvprLexerBase.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.IO;
3+
using System.Reflection;
4+
using Antlr4.Runtime;
5+
using Antlr4.Runtime.Misc;
6+
7+
public class GvprLexerBase : Lexer
8+
{
9+
protected GvprLexerBase(ICharStream input, TextWriter output, TextWriter errorOutput)
10+
: base(input, output, errorOutput)
11+
{
12+
}
13+
14+
public GvprLexerBase(ICharStream input)
15+
: base(input)
16+
{
17+
}
18+
19+
public override string[] RuleNames => throw new NotImplementedException();
20+
21+
public override IVocabulary Vocabulary => throw new NotImplementedException();
22+
23+
public override string GrammarFileName => throw new NotImplementedException();
24+
25+
public bool IsColumnZero()
26+
{
27+
return this.Column == 1;
28+
}
29+
}

gvpr/CSharp/GvprParserBase.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using Antlr4.Runtime;
2+
using System.IO;
3+
4+
public abstract class GvprParserBase : Parser {
5+
private readonly ITokenStream _input;
6+
7+
protected GvprParserBase(ITokenStream input, TextWriter output, TextWriter errorOutput)
8+
: base(input, output, errorOutput)
9+
{
10+
_input = input;
11+
}
12+
13+
public bool IsSemiRequired()
14+
{
15+
var c = (this.InputStream as CommonTokenStream).LT(-1);
16+
var d = (this.InputStream as CommonTokenStream).LT(1);
17+
return c.Type != gvprParser.CCBC;
18+
}
19+
20+
public bool IsSemiNotRequired()
21+
{
22+
var c = (this.InputStream as CommonTokenStream).LT(-1);
23+
var d = (this.InputStream as CommonTokenStream).LT(1);
24+
return c.Type == gvprParser.CCBC;
25+
}
26+
}

gvpr/Cpp/GvprLexerBase.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include "antlr4-runtime.h"
2+
#include "GvprLexerBase.h"
3+
#include "gvprLexer.h"
4+
5+
GvprLexerBase::GvprLexerBase(antlr4::CharStream * input) : antlr4::Lexer(input)
6+
{
7+
_input = input;
8+
}
9+
10+
bool GvprLexerBase::IsColumnZero()
11+
{
12+
return this->getCharPositionInLine() == 1;
13+
}

gvpr/Cpp/GvprLexerBase.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#pragma once
2+
#include "antlr4-runtime.h"
3+
4+
class GvprLexerBase : public antlr4::Lexer
5+
{
6+
public:
7+
GvprLexerBase(antlr4::CharStream * input);
8+
bool IsColumnZero();
9+
};

gvpr/Cpp/GvprParserBase.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include "gvprParser.h"
2+
3+
using namespace antlr4;
4+
5+
bool GvprParserBase::IsSemiRequired()
6+
{
7+
auto c = this->getTokenStream()->LT(-1);
8+
auto d = this->getTokenStream()->LT(1);
9+
return c->getType() != gvprParser::CCBC;
10+
}
11+
12+
bool GvprParserBase::IsSemiNotRequired()
13+
{
14+
auto c = this->getTokenStream()->LT(-1);
15+
auto d = this->getTokenStream()->LT(1);
16+
return c->getType() == gvprParser::CCBC;
17+
}

gvpr/Cpp/GvprParserBase.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#pragma once
2+
3+
#include "antlr4-runtime.h"
4+
5+
class GvprParserBase : public antlr4::Parser {
6+
public:
7+
GvprParserBase(antlr4::TokenStream *input) : Parser(input) { }
8+
bool IsSemiRequired();
9+
bool IsSemiNotRequired();
10+
};

gvpr/Cpp/transformGrammar.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import sys, os, re, shutil
2+
from glob import glob
3+
from pathlib import Path
4+
5+
def main(argv):
6+
for file in glob("./*.g4"):
7+
fix(file)
8+
9+
def fix(file_path):
10+
print("Altering " + file_path)
11+
if not os.path.exists(file_path):
12+
print(f"Could not find file: {file_path}")
13+
sys.exit(1)
14+
parts = os.path.split(file_path)
15+
file_name = parts[-1]
16+
shutil.move(file_path, file_path + ".bak")
17+
input_file = open(file_path + ".bak",'r')
18+
output_file = open(file_path, 'w')
19+
for x in input_file:
20+
if '// Insert here @header for C++ lexer.' in x:
21+
x = x.replace('// Insert here @header for C++ lexer.', '@header {#include "GvprLexerBase.h"}')
22+
if '// Insert here @header for C++ parser.' in x:
23+
x = x.replace('// Insert here @header for C++ parser.', '@header {#include "GvprParserBase.h"}')
24+
if 'this.' in x:
25+
x = x.replace('this.', 'this->')
26+
output_file.write(x)
27+
output_file.flush()
28+
29+
print("Writing ...")
30+
input_file.close()
31+
output_file.close()
32+
33+
if __name__ == '__main__':
34+
main(sys.argv)

gvpr/Dart/GvprLexerBase.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import 'package:antlr4/antlr4.dart';
2+
import 'dart:io';
3+
import 'dart:core';
4+
import 'dart:convert';
5+
import 'dart:collection';
6+
import 'gvprLexer.dart';
7+
8+
abstract class GvprLexerBase extends Lexer
9+
{
10+
GvprLexerBase(CharStream input) : super(input)
11+
{
12+
}
13+
14+
bool IsColumnZero()
15+
{
16+
return this.charPositionInLine == 1;
17+
}
18+
}

gvpr/Dart/GvprParserBase.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import 'package:antlr4/antlr4.dart';
2+
import 'dart:io';
3+
import 'dart:convert';
4+
import 'gvprParser.dart';
5+
6+
abstract class GvprParserBase extends Parser {
7+
GvprParserBase(TokenStream input) : super(input)
8+
{
9+
}
10+
11+
bool IsSemiRequired()
12+
{
13+
var c = this.tokenStream.LT(-1);
14+
var d = this.tokenStream.LT(1);
15+
return c?.type != gvprParser.TOKEN_CCBC;
16+
}
17+
18+
bool IsSemiNotRequired()
19+
{
20+
var c = this.tokenStream.LT(-1);
21+
var d = this.tokenStream.LT(1);
22+
return c?.type == gvprParser.TOKEN_CCBC;
23+
}
24+
}

gvpr/Go/gvpr_lexer_base.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package parser
2+
3+
import (
4+
"github.com/antlr4-go/antlr/v4"
5+
)
6+
7+
type GvprLexerBase struct {
8+
*antlr.BaseLexer
9+
}
10+
11+
func (l *GvprLexerBase) IsColumnZero() bool {
12+
return l.GetCharPositionInLine() == 1
13+
}

0 commit comments

Comments
 (0)