Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
078ef7f
Add token class implementation.
GabrielChiquetto Apr 25, 2019
3b8bb3d
Merge pull request #4 from Leonardo-Rocha/feature/TokenGenerator
Leonardo-Rocha Apr 25, 2019
0e921b5
Add @author in Token.java
Leonardo-Rocha Apr 26, 2019
5dd532a
Reformat all code using IntelliJ.
Leonardo-Rocha Apr 26, 2019
711ed25
Add single-line comment removal in preprocessor.
Leonardo-Rocha Apr 26, 2019
f2f32f7
Add early implementation of multi-line comment removal.
Leonardo-Rocha Apr 26, 2019
f065985
Change test file to support strange cases.
Leonardo-Rocha Apr 27, 2019
68087ac
Refactor preprocessor methods.
Leonardo-Rocha Apr 27, 2019
6c3f38a
Refactor preprocessor and main.
Leonardo-Rocha Apr 29, 2019
4dfc489
Change main.
Leonardo-Rocha Apr 29, 2019
fd8f906
Add new constructor in token.
Leonardo-Rocha Apr 29, 2019
ff77b6d
Merge pull request #5 from Leonardo-Rocha/feature/TokenGenerator
Leonardo-Rocha Apr 30, 2019
614d0a7
-change EOF to '$'
GabrielChiquetto May 1, 2019
17ba13b
Format all classes properly.
Leonardo-Rocha May 1, 2019
7082631
Add eclipse files.
May 2, 2019
54f4f45
Change Token constructors.
May 2, 2019
34da090
Add more precise error logging.
May 6, 2019
d516ba5
Remove KEywords.
May 6, 2019
b164b71
Fix some bugs.
May 8, 2019
d069aa5
Merge pull request #6 from Leonardo-Rocha/feature/TokenGenerator
Leonardo-Rocha May 8, 2019
a8b7d54
Change main class to receive a file from the command line.
Leonardo-Rocha May 17, 2019
ba1fbeb
Change file structure with packages.
Leonardo-Rocha May 24, 2019
cd37f78
Add maven structure.
Leonardo-Rocha May 24, 2019
4e6b971
Reverse commit.
Leonardo-Rocha May 25, 2019
517afa9
Fix errors.
Leonardo-Rocha May 25, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,4 @@ fabric.properties

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
/.metadata/
5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/runConfigurations/XPPCompiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [


{
"type": "java",
"name": "Debug (Launch) - Current File",
"request": "launch",
"mainClass": "${file}"
},
{
"type": "java",
"name": "Debug (Launch)-Main<java>",
"request": "launch",
"mainClass": "Main",
"projectName": "java"
}
]
}
10 changes: 10 additions & 0 deletions bin/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//this is a simple comment to test

x = /*fucking comment */ 10;
int x = 10; //será que ignora?
int y = 10;
batata = 20;
triangulo = 70;
tri%%@ 43566¨

=== 1/**/2
11 changes: 11 additions & 0 deletions bin/test_preprocessed.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@


x = 10;
int x = 10;
int y = 10;
batata = 20;
triangulo = 70;
tri%%@ 43566¨

=== 12
$
3 changes: 0 additions & 3 deletions src/java/Parser.java

This file was deleted.

3 changes: 0 additions & 3 deletions src/java/Token.java

This file was deleted.

3 changes: 0 additions & 3 deletions src/java/TokenGenerator.java

This file was deleted.

64 changes: 0 additions & 64 deletions src/java/TokenType.java

This file was deleted.

76 changes: 76 additions & 0 deletions src/java/core/CompilerMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package core;

import utils.LexicalError;
import utils.Preprocessor;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
* core.CompilerMain class.
*
* @author Leonardo-Rocha, Gabriel Chiquetto.
*/
public class CompilerMain {

public static void main(String[] args) throws IOException {

if (args.length > 0) {
File filePath = openFile(args[0]);

filePath = openFile(preprocessFile(filePath));

runTest(filePath);

LexicalError.computeErrorLog();

System.out.println("Process terminated.");
} else {
System.out.println("Please, insert a valid file path.");
}
}

/**
* @param path path of the file to be open.
* @return open file reference.
*/
private static File openFile(String path) {
return new File(path);
}

/**
* Runs a test with the TokenGenerator to visually check if it's working.
*
* @param source file to run the token generator.
* @throws IOException if an error occurs during getNextToken().
*/
private static void runTest(File source) throws IOException {
TokenGenerator tokenizer = new TokenGenerator(source);
Token currentToken = tokenizer.getNextToken();
while (!currentToken.equalsTokenType(TokenType.EOF)) {
currentToken.showCase();
currentToken = tokenizer.getNextToken();
}

}

/**
* @param rawSource source to preprocess.
* @return preprocessed file path.
*/
private static String preprocessFile(File rawSource) {
System.out.println("Test file: " + rawSource.getAbsolutePath());
System.out.println("Pre-processing test file...");
try {
Preprocessor preprocessor = new Preprocessor();
preprocessor.preProcess(rawSource);
System.out.println("Preprocess successful.");
} catch (FileNotFoundException ex) {
System.out.println("Unable to open file '" + rawSource + "'");
} catch (IOException ex) {
System.out.println("Error reading file '" + rawSource + "'");
}
return rawSource + "_preprocessed.txt";
}
}
47 changes: 47 additions & 0 deletions src/java/core/Parser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package core;

import java.io.File;
import java.io.IOException;

public class Parser {

private TokenGenerator tokenGenerator;

private Token currentToken;

private void advanceToken() throws IOException{
currentToken = this.tokenGenerator.getNextToken();
}

public Parser(File sourceCode) throws IOException {
tokenGenerator = new TokenGenerator(sourceCode);
advanceToken();
}

public void match(TokenType type){
//if(type != currentToken.getAttribute())
}

public void program(){
if(currentToken.equalsTokenType(TokenType.CLASS)){
classList();
}
}

private void classList(){
classDecl();
classListLinha();
}

private void classListLinha(){
if(currentToken.equalsTokenType(TokenType.CLASS)){
classList();
}
}

private void classDecl(){
//match(CLASS);
//match(IDENTIFIER);
//classDeclLinha();
}
}
111 changes: 111 additions & 0 deletions src/java/core/Token.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package core;

/**
* This class represents every recognized single unit(token) of the source code.
* A token is a Pair <type, attribute> and the attribute is optional.
*
* @author Gabriel Chiquetto, Leonardo-Rocha
*/
class Token {
/**
* Abstract symbol that represents a type of lexical unit.
*/
private final TokenType tokenType;
/**
* Optional value of a token.
*/
private TokenType attribute;
/**
* Sequence of characters that represents the token in the source code.
*/
private String lexeme;

/**
* Initializes a token with the main fields.
*
* @param tokenType type of the generated token.
* @param attribute value of the token.
*/
public Token(TokenType tokenType, TokenType attribute) {
this.tokenType = tokenType;
this.attribute = attribute;
}

/**
* Initializes a token without an attribute.
*
* @param tokenType type of the generated token.
*/
public Token(TokenType tokenType) {
this(tokenType, TokenType.UNDEF);
}

/**
* Override of equals method.
* @param obj Object to evaluate.
* @return true if they're equal.
*/
@Override
public boolean equals(Object obj) {
Token token = (Token) obj;
return this.getTokenType() == token.getTokenType();
}

/**
* Compare token types.
* @param tokentype
* @return true if the type of this token equals the given token type.
*/
public boolean equalsTokenType(TokenType tokentype){
return this.getTokenType() == tokentype;
}

/**
* Print token informations for debuggin purposes.
*/
public void showCase() {
System.out.print("lexeme:" + this.getLexeme());
System.out.print(", core.TokenType: " + getTokenType());
System.out.println(", Attribute: " + getAttribute() + '.');
}

/**
*
* @return type of the token.
*/
public TokenType getTokenType() {
return tokenType;
}

/**
*
* @return attribute of the token.
*/
public TokenType getAttribute() {
return attribute;
}

/**
* Set the value of the field attribute.
* @param attribute value to set.
*/
public void setAttribute(TokenType attribute){
this.attribute = attribute;
}

/**
*
* @return lexeme of the token.
*/
public String getLexeme() {
return lexeme;
}

/**
* Should only be used once.
* @param lexeme string to set.
*/
void setLexeme(String lexeme) {
this.lexeme = lexeme;
}
}
Loading