Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/antlr/grammars-v4
Browse files Browse the repository at this point in the history
  • Loading branch information
teverett committed May 14, 2024
2 parents d45e1a6 + dbaf02e commit fb68ea0
Show file tree
Hide file tree
Showing 266 changed files with 6,064 additions and 131 deletions.
2 changes: 2 additions & 0 deletions _scripts/perf-changed.sh
Original file line number Diff line number Diff line change
Expand Up @@ -301,5 +301,7 @@ EOF
cat xx.m
echo ========
cat xx.m | octave --no-gui
if [ $? -ne 0 ]; then continue; fi

done
exit 0
3 changes: 3 additions & 0 deletions aterm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# ATerm Grammar

A simple ANTLR4 grammar for [aterm](https://homepages.cwi.nl/~daybuild/daily-books/technology/aterm-guide/aterm-guide.html#section.aterm-introduction).
72 changes: 72 additions & 0 deletions aterm/aterm.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
BSD License
Copyright (c) 2024, Tom Everett
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of Tom Everett nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

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

grammar aterm;

aterm_ : term_ EOF;

term_: ( afun | num_ | function_ |placeholder | list_ ) annotation?;

afun : STRING_ | ID_;

num_: INT_ | REAL_;

function_: afun args_?;

args_: '(' termlist? ')';

placeholder: '<' term_ '>';

list_: '[' termlist? ']';

termlist: term_ (',' term_)* ;

annotation: '{' termlist? '}';

REAL_: INT_ '.' NAT EXP_?;

INT_: ('+' |'-')? NAT;

fragment NAT: [0-9]+;

fragment EXP_: (('e'|'E') INT_) | 'ε';

ID_: [a-zA-Z][a-zA-Z0-9]*;

STRING_: '"' ~'"'* '"';

WS
: [ \r\n\t]+ -> skip
;
4 changes: 4 additions & 0 deletions aterm/desc.xml
Original file line number Diff line number Diff line change
@@ -0,0 +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;Cpp;Dart;Java</targets>
</desc>
2 changes: 2 additions & 0 deletions aterm/examples/example1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"yellow"

2 changes: 2 additions & 0 deletions aterm/examples/example10.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[1,2,"abc"]

2 changes: 2 additions & 0 deletions aterm/examples/example11.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[]

2 changes: 2 additions & 0 deletions aterm/examples/example12.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[f,g([1,2]),x]

2 changes: 2 additions & 0 deletions aterm/examples/example13.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<int>

2 changes: 2 additions & 0 deletions aterm/examples/example14.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<[3]>

3 changes: 3 additions & 0 deletions aterm/examples/example15.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<f(<int>,<real>)>


3 changes: 3 additions & 0 deletions aterm/examples/example2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exam(<appl(<term>,9)>,<int>,<str>)


3 changes: 3 additions & 0 deletions aterm/examples/example3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exam(<blob>)


2 changes: 2 additions & 0 deletions aterm/examples/example4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
exam(pair(yellow,9),10, "any")

2 changes: 2 additions & 0 deletions aterm/examples/example5.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-0.7E34

2 changes: 2 additions & 0 deletions aterm/examples/example6.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
3.14

2 changes: 2 additions & 0 deletions aterm/examples/example7.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1

2 changes: 2 additions & 0 deletions aterm/examples/example8.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
f(a,b)

2 changes: 2 additions & 0 deletions aterm/examples/example9.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"test!"(1,2.1,"Hello world!")

56 changes: 56 additions & 0 deletions aterm/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>aterm</artifactId>
<packaging>jar</packaging>
<name>khubla.com ATerm grammar</name>
<parent>
<groupId>org.antlr.grammars</groupId>
<artifactId>grammarsv4</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<build>
<plugins>
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>${antlr.version}</version>
<configuration>
<sourceDirectory>${basedir}</sourceDirectory>
<includes>
<include>aterm.g4</include>
</includes>
<visitor>true</visitor>
<listener>true</listener>
</configuration>
<executions>
<execution>
<goals>
<goal>antlr4</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.khubla.antlr</groupId>
<artifactId>antlr4test-maven-plugin</artifactId>
<version>${antlr4test-maven-plugin.version}</version>
<configuration>
<verbose>false</verbose>
<showTree>false</showTree>
<entryPoint>aterm_</entryPoint>
<grammarName>aterm</grammarName>
<packageName></packageName>
<exampleFiles>examples/</exampleFiles>
</configuration>
<executions>
<execution>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
18 changes: 18 additions & 0 deletions bnf/Antlr4ng/bnfParserBase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {Parser, Lexer, Token, TokenStream, ParserRuleContext} from "antlr4ng";
import { bnfParser } from './bnfParser.js';
import { bnfLexer } from './bnfLexer.js';

export default abstract class bnfParserBase extends Parser {

constructor(input: TokenStream) {
super(input);
}

NotNL(): boolean
{
var i = 1;
var c = this.inputStream.LT(i);
var v = c.type != bnfParser.NL;
return v;
}
}
31 changes: 31 additions & 0 deletions bnf/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\+\+ parser\.)",\
'@header {import bnfParserBase from "./bnfParserBase.js"}', line)
output_file.write(line)

print("Writing ...")

if __name__ == '__main__':
main()
65 changes: 65 additions & 0 deletions bnf/CSharp/bnfParserBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using Antlr4.Runtime;
using System.IO;

public abstract class bnfParserBase : Parser {
private readonly ITokenStream _input;

protected bnfParserBase(ITokenStream input, TextWriter output, TextWriter errorOutput)
: base(input, output, errorOutput)
{
_input = input;
}

public bool NotNL()
{
int i = 1;
IToken c = (this.InputStream as CommonTokenStream).LT(i);
var v = c.Type != bnfParser.NL;
return v;
}

public bool NotAssign()
{
int i = 1;
IToken c = (this.InputStream as CommonTokenStream).LT(i);
while (c != null && c.Type == bnfLexer.WS)
c = (this.InputStream as CommonTokenStream).LT(++i);
var v = !(c.Type == bnfParser.ASSIGN1
| c.Type == bnfParser.ASSIGN2
| c.Type == bnfParser.ASSIGN3
| c.Type == bnfParser.ASSIGN4);
return v;
}

public bool NotLhs()
{
int i = 1;
IToken c = (this.InputStream as CommonTokenStream).LT(i);
while (c != null && c.Type == bnfLexer.WS)
c = (this.InputStream as CommonTokenStream).LT(++i);
if (c != null && c.Type != bnfLexer.X1)
return true;
// '<'
for (;;)
{
while (c != null && c.Type == bnfLexer.WS)
c = (this.InputStream as CommonTokenStream).LT(++i);
if (c != null && c.Type != bnfLexer.ID && c.Type != bnfLexer.X2)
return true;
// ID
if (c == null) return true;
if (c.Type == bnfLexer.X2)
break;
}
// '>'
while (c != null && c.Type == bnfLexer.WS)
c = (this.InputStream as CommonTokenStream).LT(++i);
if (c.Type == bnfLexer.ASSIGN1) return false;
if (c.Type == bnfLexer.ASSIGN2) return false;
if (c.Type == bnfLexer.ASSIGN3) return false;
if (c.Type == bnfLexer.ASSIGN4) return false;
return true;
}


}
65 changes: 65 additions & 0 deletions bnf/Dart/bnfParserBase.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import 'package:antlr4/antlr4.dart';
import 'bnfLexer.dart';

abstract class bnfParserBase extends Parser {
final TokenStream _input;

bnfParserBase(this._input)
: super(_input);

bool NotNL() {
int i = 1;
var c = (_input as CommonTokenStream).LT(i);
bool v = c?.type != bnfLexer.TOKEN_NL;
return v;
}

bool notAssign() {
int i = 1;
var c = (_input as CommonTokenStream).LT(i);
while (c != null && c.type == bnfLexer.TOKEN_WS) {
c = (_input as CommonTokenStream).LT(++i);
}
bool v = !(c?.type == bnfLexer.TOKEN_ASSIGN1 ||
c?.type == bnfLexer.TOKEN_ASSIGN2 ||
c?.type == bnfLexer.TOKEN_ASSIGN3 ||
c?.type == bnfLexer.TOKEN_ASSIGN4);
return v;
}

bool notLhs() {
int i = 1;
var c = (_input as CommonTokenStream).LT(i);
while (c != null && c.type == bnfLexer.TOKEN_WS) {
c = (_input as CommonTokenStream).LT(++i);
}
if (c != null && c.type != bnfLexer.TOKEN_X1) {
return true;
}
// '<'
while (true) {
while (c != null && c.type == bnfLexer.TOKEN_WS) {
c = (_input as CommonTokenStream).LT(++i);
}
if (c != null && c.type != bnfLexer.TOKEN_ID && c.type != bnfLexer.TOKEN_X2) {
return true;
}
// ID
if (c == null) {
return true;
}
if (c.type == bnfLexer.TOKEN_X2) {
break;
}
}
// '>'
while (c != null && c.type == bnfLexer.TOKEN_WS) {
c = (_input as CommonTokenStream).LT(++i);
}
if (c?.type == bnfLexer.TOKEN_ASSIGN1) return false;
if (c?.type == bnfLexer.TOKEN_ASSIGN2) return false;
if (c?.type == bnfLexer.TOKEN_ASSIGN3) return false;
if (c?.type == bnfLexer.TOKEN_ASSIGN4) return false;
return true;
}
}

0 comments on commit fb68ea0

Please sign in to comment.