-
Notifications
You must be signed in to change notification settings - Fork 332
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
Shared token types #2100
Draft
TwoOfTwelve
wants to merge
1
commit into
jplag:develop
Choose a base branch
from
TwoOfTwelve:feature/bachelorAlex
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Shared token types #2100
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,320 @@ | ||
Shared token types enable comparisons between languages | ||
|
||
Examples are given in a pseudo language similar to Java or Cpp | ||
|
||
# The Imperative paradigm | ||
|
||
## Variables | ||
|
||
Variable definition: | ||
``` | ||
def x: Int | ||
| VAR_DEF | ||
``` | ||
|
||
Variable definition with assignment: | ||
``` | ||
def x: Int = 2 | ||
| VAR_DEF | ||
| ASSIGN | ||
``` | ||
|
||
Assignment: | ||
``` | ||
x = 3 | ||
| ASSIGN | ||
``` | ||
|
||
## Types | ||
|
||
Structure definition: | ||
``` | ||
type Name { | ||
| STRUCT_DEF | ||
firstName: String | ||
| STRUCT_MEMBER | ||
lastName: String | ||
| STRUCT_MEMBER | ||
} | ||
| STRUCT_END | ||
``` | ||
|
||
## Control Structures | ||
|
||
If else: | ||
``` | ||
if (...) { | ||
| IF | ||
... | ||
} else { | ||
| ELSE | ||
... | ||
} | ||
| IF_END | ||
``` | ||
|
||
If without else: | ||
``` | ||
if(...) { | ||
| IF | ||
} | ||
| IF_END | ||
``` | ||
|
||
Switch-case: | ||
``` | ||
switch(...) { | ||
| SWITCH | ||
case a: { | ||
| CASE | ||
... | ||
} | ||
| CASE_END | ||
case b: { | ||
| CASE | ||
... | ||
} | ||
| CASE_END | ||
} | ||
``` | ||
|
||
Switch-case with joined-cases (like in Java): | ||
``` | ||
switch(...) { | ||
|SWITCH | ||
case a: | ||
| CASE | ||
case b: { | ||
| CASE | ||
... | ||
} | ||
| CASE_END | ||
} | ||
| SWITCH_END | ||
``` | ||
|
||
Languages that require an explicit break to end the case clause should extract the CASE_END token on the break. | ||
|
||
|
||
|
||
While-loop: | ||
``` | ||
while(...) { | ||
| LOOP | ||
| <potential tokens in condition> | ||
.... | ||
} | ||
| LOOP_END | ||
``` | ||
|
||
For-loop: | ||
``` | ||
for(def x: Int = 0; x < y; x++) { | ||
| LOOP | ||
| <potential tokens in condition> | ||
| VAR_DEF | ||
| ASSIGN | ||
... | ||
| <potential tokens in increment> | ||
} | ||
| LOOP_END | ||
``` | ||
The order of the tokens is the order of the lines here. It is intentionally different from the appearance in the code to be compatible to the while loop | ||
|
||
For-each: | ||
``` | ||
for i in <list> { | ||
| LOOP | ||
| VAR_DEF | ||
| <potential tokens in list creation> | ||
.... | ||
} | ||
| LOOP_END | ||
``` | ||
|
||
Goto: | ||
``` | ||
goto <label> | ||
|GOTO | ||
``` | ||
|
||
Break: | ||
``` | ||
break | ||
| BREAK | ||
``` | ||
|
||
Continue: | ||
``` | ||
continue | ||
| CONTINUE | ||
``` | ||
|
||
Functions: | ||
``` | ||
<modifier> function doSomething(parameter: Int): Int { | ||
| FUNCTION | VAR_DEF | ||
... | ||
return 1 | ||
| RETURN | ||
} | ||
| FUNCTION_END | ||
``` | ||
Modifiers and other features should be ignored here. All different kinds of functions (procedure, methods, functions, ...) Should be treated the same | ||
|
||
Function call: | ||
``` | ||
doSomething(1) | ||
| CALL | ||
|
||
myObject.doSomething(1) | ||
| CALL | ||
``` | ||
|
||
## Exception Handling | ||
|
||
Throw: | ||
``` | ||
throw <exception value> | ||
|THROW | ||
| <potential tokens for value> | ||
``` | ||
|
||
|
||
Try-Catch: | ||
``` | ||
try { | ||
| TRY | ||
... | ||
} | ||
catch(e: Error1) { | ||
| CATCH | ||
... | ||
} | ||
catch(e: Error2) { | ||
| CATCH | ||
... | ||
} | ||
finally { | ||
| FINALLY | ||
} | ||
|TRY_END | ||
``` | ||
There is no token for the error variable declaration, since it always has to be there and to be as language independent as possible | ||
|
||
# Object Orientation | ||
|
||
## Class / Interface definitions | ||
|
||
To maintain compatibility with other languages, all object-oriented types (enum, class, interface, abstract class, record, etc.) should be treated the same: | ||
|
||
``` | ||
class MyClass extends MyInterface { | ||
| STRUCT_DEF | ||
def attribute: Int | ||
| STRUCT_MEMBER | ||
|
||
function MyClass() { | ||
| CONST | ||
... | ||
} | ||
| CONT_END | ||
|
||
function memberFunction(): Int { | ||
| METHOD | ||
... | ||
} | ||
| METHOD_END | ||
} | ||
| STRUCT_END | ||
``` | ||
|
||
## Object creation | ||
|
||
``` | ||
def object: MyClass = new MyClass() | ||
|VAR_DEF | ||
| ASSIGN | ||
| NEW | ||
``` | ||
|
||
# Code structure | ||
|
||
``` | ||
package my_package | ||
| CONTEXT | ||
|
||
import other_package | ||
| IMPORT | ||
``` | ||
|
||
``` | ||
namespace { | ||
| CONTEXT | ||
} | ||
``` | ||
|
||
Since JPlag doesn't care about the contents of a namespace or package, it's end isn't actually relevant. The only relevant thing is that it is declared. | ||
|
||
# Assert | ||
|
||
``` | ||
assert 1 < 2 | ||
| ASSERT | ||
``` | ||
|
||
# Annotations | ||
|
||
``` | ||
@MyAnnotation | ||
| ANNOTATION | ||
function myFunction(@NotNull param: Int) { | ||
| FUNCTION | ||
| ANNOTATION | ||
| VAR_DEF | ||
... | ||
} | ||
| FUNCTION_END | ||
``` | ||
|
||
The annotation token should always appear before the annotated structure | ||
|
||
# Arrays | ||
|
||
``` | ||
def x: Int[] = new Int[1] | ||
| ARRAY_NEW | ||
|
||
def x: Int[] = new Int[]{1} | ||
| ARRAY_NEW | ||
| ARRAY_INIT_START | ||
| ARRAY_INIT_END | ||
``` | ||
|
||
# Changes with attributes | ||
|
||
``` | ||
class X { | ||
| (STRUCT_DEF, CLASS_DEF) | ||
} | ||
| (STRUCT_END, CLASS_END) | ||
``` | ||
|
||
Other types (enum, union, record) follow suit | ||
|
||
``` | ||
assert 1 < 2 | ||
| (CALL, ASSERT) | ||
``` | ||
|
||
``` | ||
def x: Int[] = new Int[]{1} | ||
| (ARRAY_NEW, NEW) | ||
| ARRAY_INIT_START | ||
| ARRAY_INIT_END | ||
``` | ||
|
||
# TODO | ||
|
||
Synchronized-Syntax | ||
Modules |
18 changes: 18 additions & 0 deletions
18
language-api/src/main/java/de/jplag/tokentypes/AnnotationTokenTypes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package de.jplag.tokentypes; | ||
|
||
import de.jplag.TokenType; | ||
|
||
public enum AnnotationTokenTypes implements TokenType { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would try avoid token types with single tokens. |
||
ANNOTATION("ANNO"); | ||
|
||
private String description; | ||
|
||
AnnotationTokenTypes(String description) { | ||
this.description = description; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return this.description; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
language-api/src/main/java/de/jplag/tokentypes/ArraySyntaxTokenTypes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package de.jplag.tokentypes; | ||
|
||
import de.jplag.TokenType; | ||
|
||
public enum ArraySyntaxTokenTypes implements TokenType { | ||
NEW_ARRAY("ARRAY_NEW"), | ||
ARRAY_INITIALIZER_START("ARRAY_INIT_START"), | ||
ARRAY_INITIALIZER_END("ARRAY_INIT_END"),; | ||
|
||
private String description; | ||
|
||
ArraySyntaxTokenTypes(String description) { | ||
this.description = description; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return this.description; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
language-api/src/main/java/de/jplag/tokentypes/AssertTokenTypes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package de.jplag.tokentypes; | ||
|
||
import de.jplag.TokenType; | ||
|
||
public enum AssertTokenTypes implements TokenType { | ||
ASSERT("ASSERT"),; | ||
|
||
private String description; | ||
|
||
AssertTokenTypes(String description) { | ||
this.description = description; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return this.description; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might be a bit too detailed for the wiki, as the wiki is currently mostly user-focused and has some high-level info for developers. However, we do not even explain tokenization in detail. Maybe we should move this documentation elsewhere, e.g. to the language API.