Skip to content

Commit

Permalink
Translated from: [antlr4-c3 java](https://github.com/mike-lischke/ant…
Browse files Browse the repository at this point in the history
  • Loading branch information
CorvusYe authored Jan 7, 2025
1 parent 91a5db0 commit e1f3333
Show file tree
Hide file tree
Showing 21 changed files with 1,727 additions and 0 deletions.
9 changes: 9 additions & 0 deletions ports/dart/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# https://dart.dev/guides/libraries/private-files
# Created by `dart pub`
.dart_tool/

# Avoid committing pubspec.lock for library packages; see
# https://dart.dev/guides/libraries/private-files#pubspeclock.
pubspec.lock

!lib/
5 changes: 5 additions & 0 deletions ports/dart/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## 1.0.0+1

## 1.0.0

- Translated from: [antlr4-c3 java](https://github.com/mike-lischke/antlr4-c3/tree/main/ports/java)
23 changes: 23 additions & 0 deletions ports/dart/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
MIT License

Copyright (c) 2024 VMware, Inc & dudu.ltd. All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

60 changes: 60 additions & 0 deletions ports/dart/README-EN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<h1 align="center"> Antlr4 Code Completion Core </h1>

<p align="center">
<a title="Pub" href="https://pub.dev/packages/antlr_c3" >
<img src="https://img.shields.io/badge/Pub-v1.x-red?style=popout" />
</a>
</p>

<p align="center">
<a title="中文"
href="https://github.com/CorvusYe/antlr4-c3/tree/main/ports/dart/README.md">
中文
</a>丨
English
</p>

This is the Dart version of the antlr4-c3 library. Translated from: [antlr4-c3 java](https://github.com/mike-lischke/antlr4-c3/tree/main/ports/java)

## Usage

Write or obtain an antlr4 grammar file, then use antlr4 to generate Dart code.

```shell
# Expr.g4 is an antlr4 grammar file, which you can write yourself or obtain from the internet
# 4.13.2 is determined by the antlr4 version in pubspec.yaml
# ../example/gen is the directory for the generated code, please modify according to your actual situation
antlr4 -v 4.13.2 -Dlanguage=Dart Expr.g4 -o ../example/gen
```

Then use the generated code in your project.

```dart
import 'dart:math';
import 'package:antlr4/antlr4.dart';
import 'package:antlr4_c3/antlr4_c3.dart';
import 'gen/ExprLexer.dart';
import 'gen/ExprParser.dart';
void main() {
var expression = 'var c = a + b()';
var lexer = ExprLexer(InputStream.fromString(expression));
var tokens = CommonTokenStream(lexer);
var parser = ExprParser(tokens);
lexer.removeErrorListeners();
parser.removeErrorListeners();
parser.expression();
var core = CodeCompletionCore(parser, null, null);
var candidates = core.collectCandidates(max(0, tokens.size), null);
print(candidates);
}
```

## License

antlr4_c3 is licensed under the MIT License.
65 changes: 65 additions & 0 deletions ports/dart/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@

<h1 align="center"> Antlr4 Code Completion Core </h1>

<p align="center">
<a title="Pub" href="https://pub.dev/packages/antlr4_c3" >
<img src="https://img.shields.io/badge/Pub-v1.x-red?style=popout" />
</a>
</p>

<p align="center">
中文丨
<a title="English"
href="https://github.com/CorvusYe/antlr4-c3/tree/main/ports/dart/README-EN.md">
English
</a>
</p>

这是antlr4-c3库的Dart版本。翻译自:[antlr4-c3 java](https://github.com/mike-lischke/antlr4-c3/tree/main/ports/java)

## 用法

编写或者获取一个antlr4的语法文件,然后使用antlr4生成Dart代码。

```shell
# Expr.g4 为 antlr4 语法文件,可以自己编写或者从网上获取
# 4.13.2 由 pubspec.yaml 中的 antlr4 版本决定
# ../example/gen 为生成的代码目录,请根据实际情况修改
antlr4 -v 4.13.2 -Dlanguage=Dart Expr.g4 -o ../example/gen
```

然后在你的代码中使用生成的代码。

```dart
import 'dart:math';
import 'package:antlr4/antlr4.dart';
import 'package:antlr4_c3/antlr4_c3.dart';
import 'gen/ExprLexer.dart';
import 'gen/ExprParser.dart';
void main() {
var expression = 'var c = a + b()';
var lexer = ExprLexer(InputStream.fromString(expression));
var tokens = CommonTokenStream(lexer);
var parser = ExprParser(tokens);
lexer.removeErrorListeners();
parser.removeErrorListeners();
parser.expression();
var core = CodeCompletionCore(parser, null, null);
var candidates = core.collectCandidates(max(0, tokens.size), null);
print(candidates);
}
```


## 开源协议

antlr4_c3 使用 MIT 协议。



30 changes: 30 additions & 0 deletions ports/dart/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This file configures the static analysis results for your project (errors,
# warnings, and lints).
#
# This enables the 'recommended' set of lints from `package:lints`.
# This set helps identify many issues that may lead to problems when running
# or consuming Dart code, and enforces writing Dart using a single, idiomatic
# style and format.
#
# If you want a smaller set of lints you can change this to specify
# 'package:lints/core.yaml'. These are just the most critical lints
# (the recommended set includes the core lints).
# The core lints are also what is used by pub.dev for scoring packages.

include: package:lints/recommended.yaml

# Uncomment the following section to specify additional rules.

# linter:
# rules:
# - camel_case_types

# analyzer:
# exclude:
# - path/to/excluded/files/**

# For more information about the core and recommended set of lints, see
# https://dart.dev/go/core-lints

# For additional information about configuring this file, see
# https://dart.dev/guides/language/analysis-options
31 changes: 31 additions & 0 deletions ports/dart/example/antlr4_c3_example.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright © 2024 VMware, Inc & dudu.ltd. All Rights Reserved.
*
* SPDX-License-Identifier: MIT
*
* See LICENSE file for more info.
*/

import 'dart:math';

import 'package:antlr4/antlr4.dart';
import 'package:antlr4_c3/antlr4_c3.dart';

import 'gen/ExprLexer.dart';
import 'gen/ExprParser.dart';

void main() {
var expression = 'var c = a + b()';
var lexer = ExprLexer(InputStream.fromString(expression));
var tokens = CommonTokenStream(lexer);
var parser = ExprParser(tokens);

lexer.removeErrorListeners();
parser.removeErrorListeners();

parser.expression();
var core = CodeCompletionCore(parser, null, null);

var candidates = core.collectCandidates(max(0, tokens.size), null);
print(candidates);
}
38 changes: 38 additions & 0 deletions ports/dart/example/gen/Expr.interp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
token literal names:
null
null
null
'+'
'-'
'*'
'/'
'='
'('
')'
null
null

token symbolic names:
null
VAR
LET
PLUS
MINUS
MULTIPLY
DIVIDE
EQUAL
OPEN_PAR
CLOSE_PAR
ID
WS

rule names:
expression
assignment
simpleExpression
variableRef
functionRef


atn:
[4, 1, 11, 42, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 2, 3, 7, 3, 2, 4, 7, 4, 1, 0, 1, 0, 3, 0, 13, 8, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 2, 1, 2, 3, 2, 23, 8, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 5, 2, 31, 8, 2, 10, 2, 12, 2, 34, 9, 2, 1, 3, 1, 3, 1, 4, 1, 4, 1, 4, 1, 4, 1, 4, 0, 1, 4, 5, 0, 2, 4, 6, 8, 0, 3, 1, 0, 1, 2, 1, 0, 3, 4, 1, 0, 5, 6, 40, 0, 12, 1, 0, 0, 0, 2, 14, 1, 0, 0, 0, 4, 22, 1, 0, 0, 0, 6, 35, 1, 0, 0, 0, 8, 37, 1, 0, 0, 0, 10, 13, 3, 2, 1, 0, 11, 13, 3, 4, 2, 0, 12, 10, 1, 0, 0, 0, 12, 11, 1, 0, 0, 0, 13, 1, 1, 0, 0, 0, 14, 15, 7, 0, 0, 0, 15, 16, 5, 10, 0, 0, 16, 17, 5, 7, 0, 0, 17, 18, 3, 4, 2, 0, 18, 3, 1, 0, 0, 0, 19, 20, 6, 2, -1, 0, 20, 23, 3, 6, 3, 0, 21, 23, 3, 8, 4, 0, 22, 19, 1, 0, 0, 0, 22, 21, 1, 0, 0, 0, 23, 32, 1, 0, 0, 0, 24, 25, 10, 4, 0, 0, 25, 26, 7, 1, 0, 0, 26, 31, 3, 4, 2, 5, 27, 28, 10, 3, 0, 0, 28, 29, 7, 2, 0, 0, 29, 31, 3, 4, 2, 4, 30, 24, 1, 0, 0, 0, 30, 27, 1, 0, 0, 0, 31, 34, 1, 0, 0, 0, 32, 30, 1, 0, 0, 0, 32, 33, 1, 0, 0, 0, 33, 5, 1, 0, 0, 0, 34, 32, 1, 0, 0, 0, 35, 36, 5, 10, 0, 0, 36, 7, 1, 0, 0, 0, 37, 38, 5, 10, 0, 0, 38, 39, 5, 8, 0, 0, 39, 40, 5, 9, 0, 0, 40, 9, 1, 0, 0, 0, 4, 12, 22, 30, 32]
18 changes: 18 additions & 0 deletions ports/dart/example/gen/Expr.tokens
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
VAR=1
LET=2
PLUS=3
MINUS=4
MULTIPLY=5
DIVIDE=6
EQUAL=7
OPEN_PAR=8
CLOSE_PAR=9
ID=10
WS=11
'+'=3
'-'=4
'*'=5
'/'=6
'='=7
'('=8
')'=9
68 changes: 68 additions & 0 deletions ports/dart/example/gen/ExprBaseListener.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Generated from Expr.g4 by ANTLR 4.13.2
// ignore_for_file: unused_import, unused_local_variable, prefer_single_quotes
import 'package:antlr4/antlr4.dart';

import 'ExprParser.dart';
import 'ExprListener.dart';


/// This class provides an empty implementation of [ExprListener],
/// which can be extended to create a listener which only needs to handle
/// a subset of the available methods.
class ExprBaseListener implements ExprListener {
/// The default implementation does nothing.
@override
void enterExpression(ExpressionContext ctx) {}

/// The default implementation does nothing.
@override
void exitExpression(ExpressionContext ctx) {}

/// The default implementation does nothing.
@override
void enterAssignment(AssignmentContext ctx) {}

/// The default implementation does nothing.
@override
void exitAssignment(AssignmentContext ctx) {}

/// The default implementation does nothing.
@override
void enterSimpleExpression(SimpleExpressionContext ctx) {}

/// The default implementation does nothing.
@override
void exitSimpleExpression(SimpleExpressionContext ctx) {}

/// The default implementation does nothing.
@override
void enterVariableRef(VariableRefContext ctx) {}

/// The default implementation does nothing.
@override
void exitVariableRef(VariableRefContext ctx) {}

/// The default implementation does nothing.
@override
void enterFunctionRef(FunctionRefContext ctx) {}

/// The default implementation does nothing.
@override
void exitFunctionRef(FunctionRefContext ctx) {}

/// The default implementation does nothing.
@override
void enterEveryRule(ParserRuleContext ctx) {}

/// The default implementation does nothing.
@override
void exitEveryRule(ParserRuleContext ctx) {}

/// The default implementation does nothing.
@override
void visitTerminal(TerminalNode node) {}

/// The default implementation does nothing.
@override
void visitErrorNode(ErrorNode node) {}
}
Loading

0 comments on commit e1f3333

Please sign in to comment.