Skip to content

Commit

Permalink
Add Cpp port, clean up CSharp code.
Browse files Browse the repository at this point in the history
  • Loading branch information
kaby76 committed Jul 17, 2023
1 parent d6353f2 commit c2937fc
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 6 deletions.
4 changes: 4 additions & 0 deletions cpp/CPP14Parser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@
* ****************************************************************************
*/
parser grammar CPP14Parser;

options {
superClass = CPP14ParserBase;
tokenVocab = CPP14Lexer;
}

// Insert here @header for C++ parser.

/*Basic concepts*/

translationUnit: declarationseq? EOF;
Expand Down
10 changes: 5 additions & 5 deletions cpp/CSharp/CPP14ParserBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

public abstract class CPP14ParserBase : Parser {

protected CPP14ParserBase(ITokenStream input, TextWriter output, TextWriter errorOutput)
: base(input, output, errorOutput)
{
}
protected CPP14ParserBase(ITokenStream input, TextWriter output, TextWriter errorOutput)
: base(input, output, errorOutput)
{
}

public bool IsPureSpecifierAllowed()
public bool IsPureSpecifierAllowed()
{
try
{
Expand Down
20 changes: 20 additions & 0 deletions cpp/Cpp/CPP14ParserBase.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "CPP14Parser.h"

using namespace antlr4;

bool CPP14ParserBase::IsPureSpecifierAllowed()
{
try
{
auto x = this->getRuleContext(); // memberDeclarator
auto c = x->children[0]->children[0];
auto c2 = c->children[0];
auto p = c2->children[1];
if (p == nullptr) return false;
return typeid(*p) == typeid(CPP14Parser::ParametersAndQualifiersContext);
}
catch (...)
{
}
return false;
}
9 changes: 9 additions & 0 deletions cpp/Cpp/CPP14ParserBase.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include "antlr4-runtime.h"

class CPP14ParserBase : public antlr4::Parser {
public:
CPP14ParserBase(antlr4::TokenStream *input) : Parser(input) { }
bool IsPureSpecifierAllowed();
};
33 changes: 33 additions & 0 deletions cpp/Cpp/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 '// Insert here @header for C++ parser.' in x:
x = x.replace('// Insert here @header for C++ parser.', '@header {#include "CPP14ParserBase.h"}')
if 'this.' in x:
x = x.replace('this.', 'this->')
output_file.write(x)
output_file.flush()

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

if __name__ == '__main__':
main(sys.argv)
2 changes: 1 addition & 1 deletion cpp/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>CSharp;Dart;Java;JavaScript</targets>
<targets>Cpp;CSharp;Dart;Java;JavaScript</targets>
</desc>

0 comments on commit c2937fc

Please sign in to comment.