Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve JavaScript target code template. #17

Merged
merged 1 commit into from Mar 7, 2021
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
17 changes: 7 additions & 10 deletions dotnet-antlr/GenBuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ echo Expected.
</Project>");
var fn = p.outputDirectory + "Test.csproj";
System.IO.File.WriteAllText(fn, Program.Localize(p.line_translation, sb.ToString()));

sb = new StringBuilder();
sb.AppendLine(@"
# Generated code from Antlr4BuildTasks.dotnet-antlr v " + Program.version + @"
Expand Down Expand Up @@ -228,19 +228,16 @@ npm install
sb = new StringBuilder();
sb.AppendLine(@"
{
""name"": ""i"",
""name"": """ + p.parser_name + @""",
""private"": true,
""version"": ""1.0.0"",
""description"": """",
""main"": ""index.js"",
""scripts"": {
""test"": ""echo \""Error: no test specified\"" && exit 1""
},
""author"": """",
""license"": ""ISC"",
""dependencies"": {
""antlr4"": ""^4.9.1"",
""fs-extra"": ""^9.1.0"",
""typescript-string-operations"": ""^1.4.1""
""get-stdin-with-tty"": ""^6.0.0""
},
""engines"": {
""node"": ""^14.8.0""
},
""type"": ""module""
}
Expand Down
92 changes: 33 additions & 59 deletions dotnet-antlr/GenMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,86 +315,63 @@ public static void main(String[] args) throws FileNotFoundException, IOExceptio
{
sb.AppendLine(@"// Template generated code from Antlr4BuildTasks.dotnet-antlr v " + Program.version);
sb.Append(@"
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const antlr4 = require('antlr4');
import antlr4 from 'antlr4';
import getStdin from 'get-stdin-with-tty';
import " + p.lexer_name + @" from './" + p.lexer_name + @".js';
import " + p.parser_name + @" from './" + p.parser_name + @".js';
const strops = require('typescript-string-operations');
let fs = require('fs-extra')

function getChar() {
let buffer = Buffer.alloc(1);
var xx = fs.readSync(0, buffer, 0, 1);
if (xx === 0) {
return '';
}
return buffer.toString('utf8');
}

class MyErrorListener extends antlr4.error.ErrorListener {
syntaxError(recognizer, offendingSymbol, line, column, msg, err) {
num_errors++;
console.error(`${offendingSymbol} line ${line}, col ${column}: ${msg}`);
}
errorCount = 0;
syntaxError(_recognizer, offendingSymbol, line, column, msg, err) {
this.errorCount++;
console.error(`${offendingSymbol} line ${line}, col ${column}: ${msg}`);
}
}

var show_tokens = false;
var show_tree = false;
var input = null;
var file_name = null;
for (let i = 2; i < process.argv.length; ++i)
{
let show_tokens = false;
let show_tree = false;
let input = null;
let file_name = null;
for (let i = 2; i < process.argv.length; ++i) {
switch (process.argv[i]) {
case '-tokens':
var show_tokens = true;
show_tokens = true;
break;
case '-tree':
var show_tree = true;
show_tree = true;
break;
case '-input':
var input = process.argv[++i];
input = process.argv[++i];
break;
case '-file':
var file_name = process.argv[++i];
file_name = process.argv[++i];
break;
default:
console.log('unknown '.concat(process.argv[i]));
}
}
var str = null;
if (input == null && file_name == null)
{
var sb = new strops.StringBuilder();
var ch;
while ((ch = getChar()) != '')
{
sb.Append(ch);
}
var input = sb.ToString();
str = antlr4.CharStreams.fromString(input);
} else if (input != null)
{

let str = null;
if (input != null) {
str = antlr4.CharStreams.fromString(input);
} else if (file_name != null)
{
} else if (file_name != null) {
str = antlr4.CharStreams.fromPathSync(file_name, 'utf8');
} else {
str = antlr4.CharStreams.fromString(await getStdin());
}
var num_errors = 0;

const lexer = new " + p.lexer_name + @"(str);
lexer.strictMode = false;
const tokens = new antlr4.CommonTokenStream(lexer);
const parser = new " + p.parser_name + @"(tokens);
const errorListener = new MyErrorListener();
lexer.removeErrorListeners();
parser.removeErrorListeners();
parser.addErrorListener(new MyErrorListener());
lexer.addErrorListener(new MyErrorListener());
if (show_tokens)
{
for (var i = 0; ; ++i)
{
var ro_token = lexer.nextToken();
var token = ro_token;
parser.addErrorListener(errorListener);
lexer.addErrorListener(errorListener);

if (show_tokens) {
for (let i = 0; ; ++i) {
let token = lexer.nextToken();
token.TokenIndex = i;
console.log(token.toString());
if (token.type === antlr4.Token.EOF)
Expand All @@ -403,17 +380,14 @@ class MyErrorListener extends antlr4.error.ErrorListener {
lexer.reset();
}
const tree = parser." + p.startRule + @"();
if (show_tree)
{
if (show_tree) {
console.log(tree.toStringTree(parser.ruleNames));
}
if (num_errors > 0)
{
if (errorListener.errorCount > 0) {
console.log('Parse failed.');
process.exitCode = 1;
}
else
{
else {
console.log('Parse succeeded.');
process.exitCode = 0;
}
Expand Down