Skip to content

Commit

Permalink
Draft Chipmunk grammar tokens for ANTLR.
Browse files Browse the repository at this point in the history
  • Loading branch information
danielperano committed Sep 24, 2024
1 parent e6351a1 commit 36efd6b
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 6 deletions.
18 changes: 12 additions & 6 deletions Lang/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@

plugins {
id 'maven-publish'
id 'org.asciidoctor.jvm.convert' version '3.1.0'
//id 'org.asciidoctor.jvm.convert' version '3.1.0'

}

apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'antlr'

group = 'myworld'
version = rootProject.ext.buildVersion
Expand All @@ -41,6 +43,7 @@ configurations {
dependencies {

implementation 'org.ow2.asm:asm:9.0'
antlr 'org.antlr:antlr4:4.13.2'

testImplementation 'org.apache.groovy:groovy:4.0.21'
testImplementation 'org.spockframework:spock-core:2.3-groovy-4.0'
Expand All @@ -61,9 +64,12 @@ java {

sourceSets {
main {
java {
srcDir 'src/main/java'
}
java {
srcDir 'src/main/java'
}
antlr {
srcDir 'src/main/antlr'
}
}

profiling {
Expand Down Expand Up @@ -119,9 +125,9 @@ publishing {
}
}

asciidoctor {
/*asciidoctor {
sourceDir 'src/doc/asciidoc'
}
}*/

clean {
delete 'build'
Expand Down
91 changes: 91 additions & 0 deletions Lang/src/main/antlr/ChipmunkLexer.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
lexer grammar ChipmunkLexer;

channels { WS_CHANNEL, NL_CHANNEL }

WS: [ t]+ -> channel(WS_CHANNEL);
NL: ('rn' | 'r' | 'n') -> channel(NL_CHANNEL);

COMMENT: '#' . NL;

BINARYLITERAL: ('0b' | '0B')[01_]+;
OCTLITERAL: ('0o' |' 0O' )[0-7_]+;
HEXLITERAL: ('0x'|'0X')[a-fA-F0-9_]+;
FLOATLITERAL: [0-9]* '.'? [0-9]+ (('e'|'E')'-'? [0-9]+)?;
INTLITERAL: [0-9][0-9_]*;
BOOLLITERAL: 'true' | 'false';

// TODO - probably need to move strings to the parser so we can handle interpolation
STRINGLITERAL: ('"' ('\\"' | [^"])* '"') | ('\'' ('\\\'' | [^'])* '\'');

LBRACE: '{';
RBRACE: '}';
LBRACKET: '[';
RBRACKET: ']';
LPAREN: '(';
RPAREN: ')';
COMMA: ',';

DOUBLECOLON: '::';
COLON: ':';
DOUBLEEQUALS: '==';
EQUALS: '=';
DOUBLEDOTLESS: '..<';
DOUBLEDOT: '..';
DOT: '.';
DOUBLESTAR: '**';
STAR: '*';
DOUBLEPLUS: '++';
PLUS: '+';
DOUBLEMINUS: '--';
MINUS: '-';
DOUBLEFSLASH: '//';
FSLASH: '/';
DOUBLEBAR: '||';
BAR: '|';
EXCLAMATION: '!';
TILDE: '~';
CARET: '^';
DOUBLELESSTHAN: '<<';
LESSTHAN: '<';
TRIPLEMORETHAN: '>>>';
DOUBLEMORETHAN: '>>';
MORETHAN: '>';
PERCENT: '%';
DOUBLEAMPERSAND: '&&';
AMPERSAND: '&';

MODULE: 'module';
FROM: 'from';
IMPORT: 'import';
AS: 'as';
IN: 'in';
CLASS: 'class';
SHARED: 'shared';
NULL: 'null';
IF: 'if';
ELSE: 'else';
FOR: 'for';
WHILE: 'while';
BREAK: 'break';
CONTINUE: 'continue';
RETURN: 'return';
TRY: 'try';
CATCH: 'catch';
FINALLY: 'finally';
THROW: 'throw';
DEF: 'def';
VAR: 'var';
TRAIT: 'trait';
FINAL: 'final';
INSTANCEOF: 'instanceof';
IS: 'is';
MATCH: 'match';
CASE: 'case';
WHEN: 'when';

// identifiers go second to last so that they don't interfere with matching keywords
IDENTIFIER: [a-zA-Z_][a-zA-Z0-9_]*;

// Antlr automatically gives us EOF, but we also want a catch-all that we can use to try to keep
// lexing/parsing past mistakes (for error analysis, tooling, etc.).
UNRECOGNIZED: .;
8 changes: 8 additions & 0 deletions Lang/src/main/antlr/ChipmunkParser.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
parser grammar ChipmunkParser;

options {
tokenVocab=ChipmunkLexer;
}

module:
EOF;
1 change: 1 addition & 0 deletions Lang/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
open module chipmunk.lang {
requires jdk.dynalink;
requires org.objectweb.asm;
requires org.antlr.antlr4.runtime;
exports chipmunk;
exports chipmunk.binary;
exports chipmunk.compiler;
Expand Down

0 comments on commit 36efd6b

Please sign in to comment.