Skip to content
Snippets Groups Projects
Commit 4a8ba03a authored by Arnaud LABOUREL's avatar Arnaud LABOUREL
Browse files

First commit

parents
No related branches found
No related tags found
No related merge requests found
Showing
with 1220 additions and 0 deletions
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/
### IntelliJ IDEA ###
.idea
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
\ No newline at end of file
# From: https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Gradle.gitlab-ci.yml
image: gradle:8.11.1-jdk23
before_script:
- GRADLE_USER_HOME="$(pwd)/.gradle"
- export GRADLE_USER_HOME
build:
stage: build
script: gradle --build-cache assemble
cache:
key: "$CI_COMMIT_REF_NAME"
policy: push
paths:
- build
- .gradle
test:
stage: test
script: gradle check
cache:
key: "$CI_COMMIT_REF_NAME"
policy: pull
paths:
- build
- .gradle
plugins {
id("java")
id("antlr")
}
group = "fr.univ_amu.l3mi"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
testImplementation("org.junit.jupiter:junit-jupiter:5.12.0")
implementation("dk.brics:automaton:1.12-4")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
testImplementation("org.assertj:assertj-core:3.27.2")
antlr("org.antlr:antlr4:4.13.2")
testImplementation("org.apache.commons:commons-exec:1.4.0")
testImplementation("org.assertj:assertj-core:3.27.2")
testImplementation("org.junit.jupiter:junit-jupiter-params:5.12.0")
}
tasks.generateGrammarSource {
arguments = arguments + listOf("-visitor")
}
tasks.test {
useJUnitPlatform()
}
\ No newline at end of file
File added
#Tue Mar 25 17:16:18 CET 2025
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.13-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
gradlew 0 → 100755
#!/bin/sh
#
# Copyright © 2015-2021 the original authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
##############################################################################
#
# Gradle start up script for POSIX generated by Gradle.
#
# Important for running:
#
# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
# noncompliant, but you have some other compliant shell such as ksh or
# bash, then to run this script, type that shell name before the whole
# command line, like:
#
# ksh Gradle
#
# Busybox and similar reduced shells will NOT work, because this script
# requires all of these POSIX shell features:
# * functions;
# * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
# «${var#prefix}», «${var%suffix}», and «$( cmd )»;
# * compound commands having a testable exit status, especially «case»;
# * various built-in commands including «command», «set», and «ulimit».
#
# Important for patching:
#
# (2) This script targets any POSIX shell, so it avoids extensions provided
# by Bash, Ksh, etc; in particular arrays are avoided.
#
# The "traditional" practice of packing multiple parameters into a
# space-separated string is a well documented source of bugs and security
# problems, so this is (mostly) avoided, by progressively accumulating
# options in "$@", and eventually passing that to Java.
#
# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
# see the in-line comments for details.
#
# There are tweaks for specific operating systems such as AIX, CygWin,
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
#
##############################################################################
# Attempt to set APP_HOME
# Resolve links: $0 may be a link
app_path=$0
# Need this for daisy-chained symlinks.
while
APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
[ -h "$app_path" ]
do
ls=$( ls -ld "$app_path" )
link=${ls#*' -> '}
case $link in #(
/*) app_path=$link ;; #(
*) app_path=$APP_HOME$link ;;
esac
done
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
APP_NAME="Gradle"
APP_BASE_NAME=${0##*/}
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
warn () {
echo "$*"
} >&2
die () {
echo
echo "$*"
echo
exit 1
} >&2
# OS specific support (must be 'true' or 'false').
cygwin=false
msys=false
darwin=false
nonstop=false
case "$( uname )" in #(
CYGWIN* ) cygwin=true ;; #(
Darwin* ) darwin=true ;; #(
MSYS* | MINGW* ) msys=true ;; #(
NONSTOP* ) nonstop=true ;;
esac
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD=$JAVA_HOME/jre/sh/java
else
JAVACMD=$JAVA_HOME/bin/java
fi
if [ ! -x "$JAVACMD" ] ; then
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
else
JAVACMD=java
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
# Increase the maximum file descriptors if we can.
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
fi
# Collect all arguments for the java command, stacking in reverse order:
# * args from the command line
# * the main class name
# * -classpath
# * -D...appname settings
# * --module-path (only if needed)
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
# For Cygwin or MSYS, switch paths to Windows format before running java
if "$cygwin" || "$msys" ; then
APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" )
JAVACMD=$( cygpath --unix "$JAVACMD" )
# Now convert the arguments - kludge to limit ourselves to /bin/sh
for arg do
if
case $arg in #(
-*) false ;; # don't mess with options #(
/?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath
[ -e "$t" ] ;; #(
*) false ;;
esac
then
arg=$( cygpath --path --ignore --mixed "$arg" )
fi
# Roll the args list around exactly as many times as the number of
# args, so each arg winds up back in the position where it started, but
# possibly modified.
#
# NB: a `for` loop captures its iteration list before it begins, so
# changing the positional parameters here affects neither the number of
# iterations, nor the values presented in `arg`.
shift # remove old arg
set -- "$@" "$arg" # push replacement arg
done
fi
# Collect all arguments for the java command;
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
# shell script including quotes and variable substitutions, so put them in
# double quotes to make sure that they get re-expanded; and
# * put everything else in single quotes, so that it's not re-expanded.
set -- \
"-Dorg.gradle.appname=$APP_BASE_NAME" \
-classpath "$CLASSPATH" \
org.gradle.wrapper.GradleWrapperMain \
"$@"
# Use "xargs" to parse quoted args.
#
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
#
# In Bash we could simply go:
#
# readarray ARGS < <( xargs -n1 <<<"$var" ) &&
# set -- "${ARGS[@]}" "$@"
#
# but POSIX shell has neither arrays nor command substitution, so instead we
# post-process each arg (as a line of input to sed) to backslash-escape any
# character that might be a shell metacharacter, then use eval to reverse
# that process (while maintaining the separation between arguments), and wrap
# the whole thing up as a single "set" statement.
#
# This will of course break if any of these variables contains a newline or
# an unmatched quote.
#
eval "set -- $(
printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
xargs -n1 |
sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
tr '\n' ' '
)" '"$@"'
exec "$JAVACMD" "$@"
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem https://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################
@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal
set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto execute
echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto execute
echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:execute
@rem Setup the command line
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd
:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
exit /b 1
:mainEnd
if "%OS%"=="Windows_NT" endlocal
:omega
rootProject.name = "python-interpreter"
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 by Bart Kiers
*
* 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.
*
* Project : python3-parser; an ANTLR4 grammar for Python 3
* https://github.com/bkiers/python3-parser
* Developed by : Bart Kiers, bart@big-o.nl
*/
// $antlr-format alignTrailingComments true, columnLimit 150, maxEmptyLinesToKeep 1, reflowComments false, useTab false
// $antlr-format allowShortRulesOnASingleLine true, allowShortBlocksOnASingleLine true, minEmptyLines 0, alignSemicolons ownLine
// $antlr-format alignColons trailing, singleLineOverrulesHangingColon true, alignLexerCommands true, alignLabels true, alignTrailers true
lexer grammar PythonLexer;
@header {
package python;
import python.interpreter.PythonLexerBase;
}
// All comments that start with "///" are copy-pasted from
// The Python Language Reference
tokens {
INDENT,
DEDENT
}
options {
superClass = PythonLexerBase;
}
// Insert here @header for C++ lexer.
/*
* lexer rules
*/
STRING: STRING_LITERAL | BYTES_LITERAL;
NUMBER: INTEGER | FLOAT_NUMBER | IMAG_NUMBER;
INTEGER: DECIMAL_INTEGER | OCT_INTEGER | HEX_INTEGER | BIN_INTEGER;
AND : 'and';
AS : 'as';
ASSERT : 'assert';
ASYNC : 'async';
AWAIT : 'await';
BREAK : 'break';
CASE : 'case';
CLASS : 'class';
CONTINUE : 'continue';
DEF : 'def';
DEL : 'del';
ELIF : 'elif';
ELSE : 'else';
EXCEPT : 'except';
FALSE : 'False';
FINALLY : 'finally';
FOR : 'for';
FROM : 'from';
GLOBAL : 'global';
IF : 'if';
IMPORT : 'import';
IN : 'in';
IS : 'is';
LAMBDA : 'lambda';
MATCH : 'match';
NONE : 'None';
NONLOCAL : 'nonlocal';
NOT : 'not';
OR : 'or';
PASS : 'pass';
RAISE : 'raise';
RETURN : 'return';
TRUE : 'True';
TRY : 'try';
UNDERSCORE : '_';
WHILE : 'while';
WITH : 'with';
YIELD : 'yield';
NEWLINE: ({this.atStartOfInput()}? SPACES | ( '\r'? '\n' | '\r' | '\f') SPACES?) {this.onNewLine();};
/// identifier ::= id_start id_continue*
NAME: ID_START ID_CONTINUE*;
/// stringliteral ::= [stringprefix](shortstring | longstring)
/// stringprefix ::= "r" | "u" | "R" | "U" | "f" | "F"
/// | "fr" | "Fr" | "fR" | "FR" | "rf" | "rF" | "Rf" | "RF"
STRING_LITERAL: ( [rR] | [uU] | [fF] | ( [fF] [rR]) | ( [rR] [fF]))? ( SHORT_STRING | LONG_STRING);
/// bytesliteral ::= bytesprefix(shortbytes | longbytes)
/// bytesprefix ::= "b" | "B" | "br" | "Br" | "bR" | "BR" | "rb" | "rB" | "Rb" | "RB"
BYTES_LITERAL: ( [bB] | ( [bB] [rR]) | ( [rR] [bB])) ( SHORT_BYTES | LONG_BYTES);
/// decimalinteger ::= nonzerodigit digit* | "0"+
DECIMAL_INTEGER: NON_ZERO_DIGIT DIGIT* | '0'+;
/// octinteger ::= "0" ("o" | "O") octdigit+
OCT_INTEGER: '0' [oO] OCT_DIGIT+;
/// hexinteger ::= "0" ("x" | "X") hexdigit+
HEX_INTEGER: '0' [xX] HEX_DIGIT+;
/// bininteger ::= "0" ("b" | "B") bindigit+
BIN_INTEGER: '0' [bB] BIN_DIGIT+;
/// floatnumber ::= pointfloat | exponentfloat
FLOAT_NUMBER: POINT_FLOAT | EXPONENT_FLOAT;
/// imagnumber ::= (floatnumber | intpart) ("j" | "J")
IMAG_NUMBER: ( FLOAT_NUMBER | INT_PART) [jJ];
DOT : '.';
ELLIPSIS : '...';
STAR : '*';
OPEN_PAREN : '(' {this.openBrace();};
CLOSE_PAREN : ')' {this.closeBrace();};
COMMA : ',';
COLON : ':';
SEMI_COLON : ';';
POWER : '**';
ASSIGN : '=';
OPEN_BRACK : '[' {this.openBrace();};
CLOSE_BRACK : ']' {this.closeBrace();};
OR_OP : '|';
XOR : '^';
AND_OP : '&';
LEFT_SHIFT : '<<';
RIGHT_SHIFT : '>>';
ADD : '+';
MINUS : '-';
DIV : '/';
MOD : '%';
IDIV : '//';
NOT_OP : '~';
OPEN_BRACE : '{' {this.openBrace();};
CLOSE_BRACE : '}' {this.closeBrace();};
LESS_THAN : '<';
GREATER_THAN : '>';
EQUALS : '==';
GT_EQ : '>=';
LT_EQ : '<=';
NOT_EQ_1 : '<>';
NOT_EQ_2 : '!=';
AT : '@';
ARROW : '->';
ADD_ASSIGN : '+=';
SUB_ASSIGN : '-=';
MULT_ASSIGN : '*=';
AT_ASSIGN : '@=';
DIV_ASSIGN : '/=';
MOD_ASSIGN : '%=';
AND_ASSIGN : '&=';
OR_ASSIGN : '|=';
XOR_ASSIGN : '^=';
LEFT_SHIFT_ASSIGN : '<<=';
RIGHT_SHIFT_ASSIGN : '>>=';
POWER_ASSIGN : '**=';
IDIV_ASSIGN : '//=';
SKIP_: ( SPACES | COMMENT | LINE_JOINING) -> skip;
UNKNOWN_CHAR: .;
/*
* fragments
*/
/// shortstring ::= "'" shortstringitem* "'" | '"' shortstringitem* '"'
/// shortstringitem ::= shortstringchar | stringescapeseq
/// shortstringchar ::= <any source character except "\" or newline or the quote>
fragment SHORT_STRING:
'\'' (STRING_ESCAPE_SEQ | ~[\\\r\n\f'])* '\''
| '"' ( STRING_ESCAPE_SEQ | ~[\\\r\n\f"])* '"'
;
/// longstring ::= "'''" longstringitem* "'''" | '"""' longstringitem* '"""'
fragment LONG_STRING: '\'\'\'' LONG_STRING_ITEM*? '\'\'\'' | '"""' LONG_STRING_ITEM*? '"""';
/// longstringitem ::= longstringchar | stringescapeseq
fragment LONG_STRING_ITEM: LONG_STRING_CHAR | STRING_ESCAPE_SEQ;
/// longstringchar ::= <any source character except "\">
fragment LONG_STRING_CHAR: ~'\\';
/// stringescapeseq ::= "\" <any source character>
fragment STRING_ESCAPE_SEQ: '\\' . | '\\' NEWLINE;
/// nonzerodigit ::= "1"..."9"
fragment NON_ZERO_DIGIT: [1-9];
/// digit ::= "0"..."9"
fragment DIGIT: [0-9];
/// octdigit ::= "0"..."7"
fragment OCT_DIGIT: [0-7];
/// hexdigit ::= digit | "a"..."f" | "A"..."F"
fragment HEX_DIGIT: [0-9a-fA-F];
/// bindigit ::= "0" | "1"
fragment BIN_DIGIT: [01];
/// pointfloat ::= [intpart] fraction | intpart "."
fragment POINT_FLOAT: INT_PART? FRACTION | INT_PART '.';
/// exponentfloat ::= (intpart | pointfloat) exponent
fragment EXPONENT_FLOAT: ( INT_PART | POINT_FLOAT) EXPONENT;
/// intpart ::= digit+
fragment INT_PART: DIGIT+;
/// fraction ::= "." digit+
fragment FRACTION: '.' DIGIT+;
/// exponent ::= ("e" | "E") ["+" | "-"] digit+
fragment EXPONENT: [eE] [+-]? DIGIT+;
/// shortbytes ::= "'" shortbytesitem* "'" | '"' shortbytesitem* '"'
/// shortbytesitem ::= shortbyteschar | bytesescapeseq
fragment SHORT_BYTES:
'\'' (SHORT_BYTES_CHAR_NO_SINGLE_QUOTE | BYTES_ESCAPE_SEQ)* '\''
| '"' ( SHORT_BYTES_CHAR_NO_DOUBLE_QUOTE | BYTES_ESCAPE_SEQ)* '"'
;
/// longbytes ::= "'''" longbytesitem* "'''" | '"""' longbytesitem* '"""'
fragment LONG_BYTES: '\'\'\'' LONG_BYTES_ITEM*? '\'\'\'' | '"""' LONG_BYTES_ITEM*? '"""';
/// longbytesitem ::= longbyteschar | bytesescapeseq
fragment LONG_BYTES_ITEM: LONG_BYTES_CHAR | BYTES_ESCAPE_SEQ;
/// shortbyteschar ::= <any ASCII character except "\" or newline or the quote>
fragment SHORT_BYTES_CHAR_NO_SINGLE_QUOTE:
[\u0000-\u0009]
| [\u000B-\u000C]
| [\u000E-\u0026]
| [\u0028-\u005B]
| [\u005D-\u007F]
;
fragment SHORT_BYTES_CHAR_NO_DOUBLE_QUOTE:
[\u0000-\u0009]
| [\u000B-\u000C]
| [\u000E-\u0021]
| [\u0023-\u005B]
| [\u005D-\u007F]
;
/// longbyteschar ::= <any ASCII character except "\">
fragment LONG_BYTES_CHAR: [\u0000-\u005B] | [\u005D-\u007F];
/// bytesescapeseq ::= "\" <any ASCII character>
fragment BYTES_ESCAPE_SEQ: '\\' [\u0000-\u007F];
fragment SPACES: [ \t]+;
fragment COMMENT: '#' ~[\r\n\f]*;
fragment LINE_JOINING: '\\' SPACES? ( '\r'? '\n' | '\r' | '\f');
// TODO: ANTLR seems lack of some Unicode property support...
//$ curl https://www.unicode.org/Public/13.0.0/ucd/PropList.txt | grep Other_ID_
//1885..1886 ; Other_ID_Start # Mn [2] MONGOLIAN LETTER ALI GALI BALUDA..MONGOLIAN LETTER ALI GALI THREE BALUDA
//2118 ; Other_ID_Start # Sm SCRIPT CAPITAL P
//212E ; Other_ID_Start # So ESTIMATED SYMBOL
//309B..309C ; Other_ID_Start # Sk [2] KATAKANA-HIRAGANA VOICED SOUND MARK..KATAKANA-HIRAGANA SEMI-VOICED SOUND MARK
//00B7 ; Other_ID_Continue # Po MIDDLE DOT
//0387 ; Other_ID_Continue # Po GREEK ANO TELEIA
//1369..1371 ; Other_ID_Continue # No [9] ETHIOPIC DIGIT ONE..ETHIOPIC DIGIT NINE
//19DA ; Other_ID_Continue # No NEW TAI LUE THAM DIGIT ONE
fragment UNICODE_OIDS: '\u1885' ..'\u1886' | '\u2118' | '\u212e' | '\u309b' ..'\u309c';
fragment UNICODE_OIDC: '\u00b7' | '\u0387' | '\u1369' ..'\u1371' | '\u19da';
/// id_start ::= <all characters in general categories Lu, Ll, Lt, Lm, Lo, Nl, the underscore, and characters with the Other_ID_Start property>
fragment ID_START:
'_'
| [\p{L}]
| [\p{Nl}]
//| [\p{Other_ID_Start}]
| UNICODE_OIDS
;
/// id_continue ::= <all characters in id_start, plus characters in the categories Mn, Mc, Nd, Pc and others with the Other_ID_Continue property>
fragment ID_CONTINUE:
ID_START
| [\p{Mn}]
| [\p{Mc}]
| [\p{Nd}]
| [\p{Pc}]
//| [\p{Other_ID_Continue}]
| UNICODE_OIDC
;
\ No newline at end of file
parser grammar PythonParser;
@header {
package python;
}
options { tokenVocab=PythonLexer; }
file_input
: (NEWLINE | statement)* EOF
;
statement
: simple_statement NEWLINE # simpleStatement
| WHILE expression COLON block # whileStatement
;
simple_statement:
expression # expressionStatement
| NAME ASSIGN expression # assignment
;
block
: NEWLINE INDENT statement+ DEDENT # listBlock
;
expression
: NAME # variable
| NUMBER # literalNumber
| TRUE # true
| FALSE # false
| NONE # none
| NAME OPEN_PAREN CLOSE_PAREN # functionCallWithoutArgument
| NAME OPEN_PAREN expression (COMMA expression)* CLOSE_PAREN # functionCallWithArguments
| expression ADD expression # addition
| expression LESS_THAN expression # lessThan
| OPEN_PAREN expression CLOSE_PAREN # betweenParenthesesExpression
| NOT expression # not
;
package python.interpreter;
import org.antlr.v4.gui.TreeViewer;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.TokenStream;
import python.PythonLexer;
import python.PythonParser;
import java.util.Arrays;
import java.util.Objects;
public class MainPython {
public static void main(String[] args) {
try {
CharStream stream = CharStreams
.fromStream(Objects.requireNonNull(ClassLoader.getSystemResourceAsStream("test.py")));
PythonLexer lexer = new PythonLexer(stream);
TokenStream tokenStream = new CommonTokenStream(lexer);
PythonParser parser = new PythonParser(tokenStream);
var tree = parser.file_input();
PythonInterpreterVisitor pythonInterpreterVisitor = new PythonInterpreterVisitor();
tree.accept(pythonInterpreterVisitor);
TreeViewer viewer = new TreeViewer(Arrays.asList(parser.getRuleNames()), tree);
viewer.open();
} catch (Exception e) {
e.printStackTrace();
}
}
}
package python.interpreter;
import org.antlr.v4.runtime.tree.TerminalNode;
import python.PythonParser;
import python.PythonParserBaseVisitor;
import python.interpreter.exception.IllegalNumberOfArguments;
import python.interpreter.exception.UnknownFunctionException;
import python.interpreter.exception.UnknownVariableException;
import python.interpreter.function.FunctionTable;
import python.interpreter.function.PythonFunction;
import python.interpreter.value.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class PythonInterpreterVisitor extends PythonParserBaseVisitor<PythonValue> {
private final VariableTable variableTable = new VariableTable();
private final FunctionTable functionTable = new FunctionTable();
@Override
public PythonValue visitFile_input(PythonParser.File_inputContext ctx) {
for (var node : ctx.statement()) {
node.accept(this);
}
return PythonNone.NONE;
}
@Override
public PythonValue visitSimpleStatement(PythonParser.SimpleStatementContext ctx) {
return ctx.simple_statement().accept(this);
}
@Override
public PythonValue visitWhileStatement(PythonParser.WhileStatementContext ctx) {
var conditionNode = ctx.expression();
while (conditionNode.accept(this).isTrue()) {
ctx.block().accept(this);
}
return PythonNone.NONE;
}
@Override
public PythonValue visitExpressionStatement(PythonParser.ExpressionStatementContext ctx) {
return ctx.expression().accept(this);
}
@Override
public PythonValue visitFunctionCallWithoutArgument(PythonParser.FunctionCallWithoutArgumentContext ctx) {
int line = ctx.start.getLine();
PythonFunction function = getPythonFunction(ctx.NAME(), line);
List<PythonValue> arguments = List.of();
return executeFunction(function, arguments, line);
}
private PythonValue executeFunction(PythonFunction function, List<PythonValue> arguments, int line) {
try {
return function.execute(this, arguments);
} catch (IllegalNumberOfArguments e) {
e.setLine(line);
throw e;
}
}
private PythonFunction getPythonFunction(TerminalNode functionNode, int line) {
String functionName = functionNode.getText();
Optional<PythonFunction> function = functionTable.getFunction(functionName);
if (function.isEmpty()) {
throw new UnknownFunctionException(line, functionName);
}
return function.get();
}
@Override
public PythonValue visitFunctionCallWithArguments(PythonParser.FunctionCallWithArgumentsContext ctx) {
int line = ctx.start.getLine();
PythonFunction function = getPythonFunction(ctx.NAME(), line);
List<PythonValue> arguments = getArguments(ctx);
return executeFunction(function, arguments, line);
}
private List<PythonValue> getArguments(PythonParser.FunctionCallWithArgumentsContext ctx) {
List<PythonValue> arguments = new ArrayList<>();
for(var argumentNode : ctx.expression())
arguments.add(argumentNode.accept(this));
return arguments;
}
@Override
public PythonValue visitAssignment(PythonParser.AssignmentContext ctx) {
String variableName = ctx.NAME().getText();
PythonValue value = ctx.expression().accept(this);
variableTable.assignVariable(variableName, value);
return PythonNone.NONE;
}
@Override
public PythonValue visitListBlock(PythonParser.ListBlockContext ctx) {
for (var statement : ctx.statement()) {
statement.accept(this);
}
return PythonNone.NONE;
}
@Override
public PythonValue visitLessThan(PythonParser.LessThanContext ctx) {
PythonValue value1 = ctx.expression(0).accept(this);
PythonValue value2 = ctx.expression(1).accept(this);
return new PythonBoolean(value1.isLessThan(value2));
}
@Override
public PythonValue visitLiteralNumber(PythonParser.LiteralNumberContext ctx) {
String literalText = ctx.NUMBER().getText();
return new PythonInteger(literalText);
}
@Override
public PythonValue visitVariable(PythonParser.VariableContext ctx) {
String variableName = ctx.NAME().getText();
Optional<PythonValue> value = variableTable.getVariableValue(variableName);
if (value.isPresent()) {
return value.get();
}
throw new UnknownVariableException(ctx.start.getLine(), variableName);
}
@Override
public PythonValue visitAddition(PythonParser.AdditionContext ctx) {
PythonValue value1 = ctx.expression(0).accept(this);
PythonValue value2 = ctx.expression(1).accept(this);
return value1.plus(value2);
}
@Override
public PythonValue visitBetweenParenthesesExpression(PythonParser.BetweenParenthesesExpressionContext ctx) {
return ctx.expression().accept(this);
}
@Override
public PythonValue visitTrue(PythonParser.TrueContext ctx) {
return new PythonBoolean(true);
}
@Override
public PythonValue visitFalse(PythonParser.FalseContext ctx) {
return new PythonBoolean(false);
}
@Override
public PythonValue visitNone(PythonParser.NoneContext ctx) {
return PythonNone.NONE;
}
@Override
public PythonValue visitNot(PythonParser.NotContext ctx) {
return new PythonBoolean(!ctx.expression().accept(this).isTrue());
}
}
package python.interpreter;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CommonToken;
import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.Token;
import python.PythonLexer;
import java.util.ArrayDeque;
import java.util.Deque;
public abstract class PythonLexerBase extends Lexer {
// A queue where extra tokens are pushed on (see the NEWLINE lexer rule).
private java.util.LinkedList<Token> tokens = new java.util.LinkedList<>();
// The stack that keeps track of the indentation level.
private Deque<Integer> indents = new ArrayDeque<>();
// The amount of opened braces, brackets and parenthesis.
private int opened = 0;
// The most recently produced token.
private Token lastToken = null;
protected PythonLexerBase(CharStream input) {
super(input);
}
@Override
public void emit(Token t) {
super.setToken(t);
tokens.offer(t);
}
@Override
public Token nextToken() {
// Check if the end-of-file is ahead and there are still some DEDENTS expected.
if (_input.LA(1) == EOF && !this.indents.isEmpty()) {
// Remove any trailing EOF tokens from our buffer.
for (int i = tokens.size() - 1; i >= 0; i--) {
if (tokens.get(i).getType() == EOF) {
tokens.remove(i);
}
}
// First emit an extra line break that serves as the end of the statement.
this.emit(commonToken(PythonLexer.NEWLINE, "\n"));
// Now emit as much DEDENT tokens as needed.
while (!indents.isEmpty()) {
this.emit(createDedent());
indents.pop();
}
// Put the EOF back on the token stream.
this.emit(commonToken(PythonLexer.EOF, "<EOF>"));
}
Token next = super.nextToken();
if (next.getChannel() == Token.DEFAULT_CHANNEL) {
// Keep track of the last token on the default channel.
this.lastToken = next;
}
return tokens.isEmpty() ? next : tokens.poll();
}
private Token createDedent() {
CommonToken dedent = commonToken(PythonLexer.DEDENT, "");
dedent.setLine(this.lastToken.getLine());
return dedent;
}
private CommonToken commonToken(int type, String text) {
int stop = this.getCharIndex() - 1;
int start = text.isEmpty() ? stop : stop - text.length() + 1;
return new CommonToken(this._tokenFactorySourcePair, type, DEFAULT_TOKEN_CHANNEL, start, stop);
}
// Calculates the indentation of the provided spaces, taking the
// following rules into account:
//
// "Tabs are replaced (from left to right) by one to eight spaces
// such that the total number of characters up to and including
// the replacement is a multiple of eight [...]"
//
// -- https://docs.python.org/3.1/reference/lexical_analysis.html#indentation
static int getIndentationCount(String spaces) {
int count = 0;
for (char ch : spaces.toCharArray()) {
if (ch == '\t') {
count += 8 - (count % 8);
} else {// A normal space char.
count++;
}
}
return count;
}
protected boolean atStartOfInput() {
return super.getCharPositionInLine() == 0 && super.getLine() == 1;
}
public void openBrace(){
this.opened++;
}
protected void closeBrace(){
this.opened--;
}
public void onNewLine(){
String newLine = getText().replaceAll("[^\r\n\f]+", "");
String spaces = getText().replaceAll("[\r\n\f]+", "");
// Strip newlines inside open clauses except if we are near EOF. We keep NEWLINEs near EOF to
// satisfy the final newline needed by the single_put rule used by the REPL.
int next = _input.LA(1);
int nextnext = _input.LA(2);
if (opened > 0 || (nextnext != -1 && (next == '\r' || next == '\n' || next == '\f' || next == '#'))) {
// If we're inside a list or on a blank line, ignore all indents,
// dedents and line breaks.
skip();
}
else {
emit(commonToken(PythonLexer.NEWLINE, newLine));
int indent = getIndentationCount(spaces);
int previous = indents.isEmpty() ? 0 : indents.peek();
if (indent == previous) {
// skip indents of the same size as the present indent-size
skip();
}
else if (indent > previous) {
indents.push(indent);
emit(commonToken(PythonLexer.INDENT, spaces));
}
else {
// Possibly emit more than 1 DEDENT token.
while(!indents.isEmpty() && indents.peek() > indent) {
this.emit(createDedent());
indents.pop();
}
}
}
}
@Override
public void reset()
{
tokens = new java.util.LinkedList<>();
indents = new ArrayDeque<>();
opened = 0;
lastToken = null;
super.reset();
}
}
\ No newline at end of file
package python.interpreter.exception;
import python.interpreter.value.PythonValue;
public class BinaryOperationTypeException extends ParserException {
public BinaryOperationTypeException(String operation, PythonValue value1, PythonValue value2) {
super(0, "TypeError: unsupported operand type(s) for " + operation + ": " + value1.getType() + " and " + value2.getType());
}
}
package python.interpreter.exception;
import python.interpreter.function.PythonFunction;
public class IllegalNumberOfArguments extends ParserException {
public IllegalNumberOfArguments(int line, PythonFunction function, int actualNumberOfArguments) {
super(line, "function " + function.getName() + " expected " + function.getExpectedNumberOfArguments() + " argument(s) but received " + actualNumberOfArguments + " argument(s)");
}
}
package python.interpreter.exception;
public class ParserException extends RuntimeException {
private int line;
private final String error;
public ParserException(int line, String errorParserMessage) {
this.line = line;
this.error = errorParserMessage;
}
public void setLine(int line) {
this.line = line;
}
private String getLineNumber(){
if(line < 0){
return "unknown";
}
return Integer.toString(line);
}
@Override
public String getMessage() {
return "Line " + getLineNumber() + ": " + error;
}
}
package python.interpreter.exception;
import python.interpreter.value.PythonValue;
public class UnaryOperationTypeException extends ParserException {
public UnaryOperationTypeException(String operation, PythonValue value) {
super(0, "operation " + operation + " not permitted on " + value.getType());
}
}
package python.interpreter.exception;
public class UnknownFunctionException extends ParserException {
public UnknownFunctionException(int line, String functionName) {
super(line, "unknown function " + functionName);
}
}
package python.interpreter.exception;
public class UnknownVariableException extends ParserException {
public UnknownVariableException(int line, String variableName) {
super(line, "Unknown variable " + variableName);
}
}
package python.interpreter.function;
import java.util.HashMap;
import java.util.Optional;
public class FunctionTable {
HashMap<String, PythonFunction> table = new HashMap<>();
public FunctionTable() {
addFunction(new PrintPythonFunction());
}
public Optional<PythonFunction> getFunction(String functionName) {
return Optional.ofNullable(table.get(functionName));
}
public void addFunction(PythonFunction function) {
table.put(function.getName(), function);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment