Skip to content

Commit 21315e0

Browse files
author
Mohamed Nasser
committedMar 15, 2018
(Add) test cases , rw.js to read and write to files.
(Fix) fix some bugs by reordering
1 parent bb4f710 commit 21315e0

13 files changed

+309
-65
lines changed
 

Diff for: ‎.getignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
RESULTS/

Diff for: ‎.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
RESULTS/TESTCASE1.txt
2+
RESULTS/TESTCASE2.txt
3+
RESULTS/TESTCASE3.txt

Diff for: ‎TESTCASES/TESTCASE1-ANSWER.txt

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
< CLASS > : -class-
2+
< ID > : -class1-
3+
< LEFT_CURLY_B > : - { -
4+
< INT > : -int-
5+
< ID > : -value-
6+
< ASSIGNMENT > : -=-
7+
< INTEGER_LITERAL > : 3
8+
< SEMICOLON > : -;-
9+
< STRING > : -String-
10+
< ID > : -text-
11+
< ASSIGNMENT > : -=-
12+
< STRING_LITERAL > : -"int x = 1"-
13+
< SEMICOLON > : -;-
14+
< M_COMMENTS > : -/*
15+
if (num < 1)
16+
num = num +1 ;
17+
*/-
18+
< PRIVATE > : -private-
19+
< VOID > : -void
20+
< ID > : print
21+
< LEFT_ROUND_B > : -(-
22+
< RIGHT_ROUND_B > : -)-
23+
< LEFT_CURLY_B > : -{-
24+
< SYSTEM.OUT.PRINTLN > : -System.out.println-
25+
< LEFT_ROUND_B > : -(-
26+
< STRING_LITERAL > : "/*Hello*/"
27+
< RIGHT_ROUND_B > : -)-
28+
< SEMICOLON > : -;-
29+
< RIGHT_CURLY_B > : -}-
30+
< RIGHT_CURLY_B > : -}-
31+
< SEMICOLON > : -;-
32+
< EOL > : -ENDOFLINE-

Diff for: ‎TESTCASES/TESTCASE1.txt

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class class1 {
2+
int value = 3;
3+
String text = "int x = 1";
4+
/*
5+
if (num < 1)
6+
num = num +1 ;
7+
*/
8+
private void print()
9+
{
10+
System.out.println("/*hello*/");
11+
}
12+
};

Diff for: ‎TESTCASES/TESTCASE2-ANSWER.txt

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
< CLASS > : class
2+
< ID > : Factorial
3+
< LEFT_CURLY_B > : {
4+
< PUBLIC > : public
5+
< STATIC > : static
6+
< VOID > : void
7+
< MAIN > : main
8+
< LEFT_ROUND_B > : (
9+
< STRING > : String
10+
< LEFT_SQUARE_B > : [
11+
< RIGHT_SQUARE_B > : ]
12+
< ID > : a
13+
< RIGHT_ROUND_B > : )
14+
< LEFT_CURLY_B > : {
15+
< SYSTEM.OUT.PRINTLN > : System.out.println
16+
< LEFT_ROUND_B > : (
17+
< NEW > : new
18+
< ID > : Fac
19+
< LEFT_ROUND_B > : (
20+
< RIGHT_ROUND_B > : )
21+
< DOT > : .
22+
< ID > : ComputeFac
23+
< LEFT_ROUND_B > : (
24+
< INTEGER_LITERAL > : 10
25+
< RIGHT_ROUND_B > : )
26+
< RIGHT_ROUND_B > : )
27+
< SEMICOLON > : ;
28+
< RIGHT_CURLY_B > : }
29+
< EOL > : End of line
30+
< RIGHT_CURLY_B > : }
31+
< EOL > : End of line
32+
< EOL > : End of line
33+
< CLASS > : class
34+
< ID > : Fac
35+
< LEFT_CURLY_B > : {
36+
< PUBLIC > : public
37+
< INT > : int
38+
< ID > : ComputeFac
39+
< LEFT_ROUND_B > : (
40+
< INT > : int
41+
< ID > : num
42+
< RIGHT_ROUND_B > : )
43+
< LEFT_CURLY_B > : {
44+
< INT > : int
45+
< ID > : num_aux
46+
< SEMICOLON > : ;
47+
< IF > : if
48+
< LEFT_ROUND_B > : (
49+
< ID > : num
50+
< LESSTHAN > : <
51+
< INTEGER_LITERAL > : 1
52+
< RIGHT_ROUND_B > : )
53+
< ID > : num_aux
54+
< ASSIGNMENT > : =
55+
< INTEGER_LITERAL > : 1
56+
< SEMICOLON > : ;
57+
< ELSE > : else
58+
< EOL > : End of line
59+
< EOL > : End of line
60+
< ID > : num_aux
61+
< ASSIGNMENT > : =
62+
< ID > : num
63+
< MULTIPLIY > : *
64+
< LEFT_ROUND_B > : (
65+
< THIS > : this
66+
< DOT > : .
67+
< ID > : ComputeFac
68+
< LEFT_ROUND_B > : (
69+
< ID > : num
70+
< PLUS > : +
71+
< INTEGER_LITERAL > : 1
72+
< RIGHT_ROUND_B > : )
73+
< RIGHT_ROUND_B > : )
74+
< SEMICOLON > : ;
75+
< RETURN > : return
76+
< ID > : num_aux
77+
< SEMICOLON > : ;
78+
< RIGHT_CURLY_B > : }
79+
< EOL > : End of line
80+
< RIGHT_CURLY_B > : }
81+
< EOL > : End of line

Diff for: ‎TESTCASES/TESTCASE2.txt

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Factorial{
2+
public static void main(String[] a){ System.out.println(new Fac().ComputeFac(10));
3+
}
4+
}
5+
6+
class Fac {
7+
public int ComputeFac(int num){
8+
int num_aux ;
9+
if (num < 1)
10+
num_aux = 1 ;
11+
else
12+
13+
14+
num_aux = num * (this.ComputeFac(num+1)) ;
15+
return num_aux ;
16+
}
17+
}

Diff for: ‎TESTCASES/TESTCASE3-ANSWER.txt

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
ERROR: '3CC1' This token did not match any RE @ index 70
2+
< CLASS > : -class-
3+
< ID > : -myclass-
4+
< LEFT_CURLY_B > : -{-
5+
< M_COMMENTS > : -/* my ultimate comment */-
6+
< EOL > : - End of Line -
7+
< INT > : -int-
8+
< ID > : -var-
9+
< RIGHT_ROUND_B > : -)-
10+
< RIGHT_ROUND_B > : -)-
11+
< ID > : -XX1-
12+
< ASSIGNMENT > : -=-
13+
< INTEGER_LITERAL > : -0-
14+
< SEMICOLON > : -;-
15+
< MULTIPLIY > : -*-
16+
< DIV > : -/-
17+
< INT > : -int-
18+
< RIGHT_ROUND_B > : -)-
19+
< ASSIGNMENT > : -=-
20+
< INTEGER_LITERAL > : -0-
21+
< SEMICOLON > : -;-
22+
< MULTIPLIY > : -*-
23+
< DIV > : -/-
24+
< INTEGER_LITERAL > : -10-
25+
< PLUS > : -+-
26+
< INTEGER_LITERAL > : -5-
27+
< SEMICOLON > : -;-
28+
< EOL > : - End of Line -
29+
< RIGHT_CURLY_B > : -}-
30+
< SEMICOLON > : -;-
31+
< EOL > : - End of Line -

Diff for: ‎TESTCASES/TESTCASE3.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class myclass{
2+
/* my ultimate comment */
3+
int var)) XX1 = 0; */
4+
int 3CC1) = 0; */
5+
10+5;
6+
};

Diff for: ‎nmjla.js

+70-35
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,87 @@
1+
const rw = require("./rw");
12
// set of regexps to match reserved words
2-
const reservedWords = require('./reservedWords');
3+
const reservedWords = require("./reservedWords");
34
// set of regexps to match operations
4-
const operations = require('./operations');
5+
const operations = require("./operations");
56
// set of regexps to match types ( string int ..)
6-
const types = require('./types');
7-
// regexp for ids
7+
const types = require("./types");
8+
// regexp for ids
89
const id = /[a-zA-z]\w*/g;
910

1011
// array to collect all tokens
11-
let allTokens = [];
1212

13-
// the source code
14-
let str = "class** nh if elsemyclass{}; /* kdjaskj */ \"jddasj\" 9-9 5.6 9 int x5 for i=1\n";
13+
// helper function
14+
const splitAt = index => x => [x.slice(0, index), x.slice(index)];
1515

16-
// collect reserved words
17-
types.forEach(e =>{
18-
while (match = e.reg.exec(str)) {
16+
let tests = [
17+
'class class1 {\n int value = 3;\n String text = "int x = 1";\n /*\n if (num < 1)\n num = num +1 ;\n */\n private void print() \n {\n System.out.println("/*hello*/"); \n }\n};',
18+
"class Factorial{\npublic static void main(String[] a){ System.out.println(new Fac().ComputeFac(10));\n}\n}\n\nclass Fac {\npublic int ComputeFac(int num){\nint num_aux ;\nif (num < 1)\nnum_aux = 1 ; \nelse\n \n\nnum_aux = num * (this.ComputeFac(num+1)) ; \nreturn num_aux ;\n}\n}",
19+
"class myclass{\n/* my ultimate comment */\nint var)) XX1 = 0; */\nint 3CC1) = 0; */\n10+5;\n};"
20+
];
21+
22+
let testPaathes = [ 'TESTCASES/TESTCASE1.txt' , 'TESTCASES/TESTCASE2.txt' , 'TESTCASES/TESTCASE3.txt'].forEach(path => {
23+
// the source code
24+
rw.getFileContent(path, str => {
25+
let allTokens = [];
26+
27+
console.log(
28+
"\n||||||||||||||||||||||| TEST ||||||||||||||||||||||||||||||||||\n"
29+
);
30+
31+
// collect types
32+
types.forEach(e => {
33+
while ((match = e.reg.exec(str))) {
1934
let matched = match[2] ? match[2].toString() : match.toString();
20-
allTokens.push({index:match.index , type: e.name , token: matched });
21-
str = str.replace( matched , ' '.repeat( matched.length ) );
22-
}
23-
});
35+
allTokens.push({ index: match.index, type: e.name, token: matched });
36+
str = splitAt(match.index)(str);
37+
str[1] = str[1].replace(matched, " ".repeat(matched.length));
38+
str = str.join("");
39+
}
40+
});
2441

25-
// collect reserved words
26-
reservedWords.all.forEach(e =>{
27-
while (match = e.reg.exec(str)) {
28-
allTokens.push({index:match.index , type: e.name , token: match[2].toString() });
29-
}
30-
});
42+
// collect reserved words
43+
reservedWords.all.forEach(e => {
44+
while ((match = e.reg.exec(str))) {
45+
allTokens.push({
46+
index: match.index,
47+
type: e.name,
48+
token: match[2].toString()
49+
});
50+
}
51+
});
3152

32-
// collect operations
33-
operations.forEach(e =>{
34-
while (match = e.reg.exec(str)) {
35-
allTokens.push({index:match.index , type: e.name , token: match.toString() });
36-
}
37-
});
53+
// collect operations
54+
operations.forEach(e => {
55+
while ((match = e.reg.exec(str))) {
56+
allTokens.push({
57+
index: match.index,
58+
type: e.name,
59+
token: match.toString()
60+
});
61+
}
62+
});
3863

39-
// collect ids
40-
while (match = id.exec(str)) {
41-
if(new RegExp(reservedWords.sum,'g').exec(match.toString()).index != 1){
42-
allTokens.push({index:match.index, type: 'ID' , token: match[0].toString() });
64+
// collect ids
65+
while ((match = id.exec(str))) {
66+
if (
67+
new RegExp(reservedWords.sum, "g").exec(match.toString()).index != 1
68+
) {
69+
allTokens.push({
70+
index: match.index,
71+
type: "ID",
72+
token: match[0].toString()
73+
});
74+
}
4375
}
44-
}
4576

77+
allTokens.sort((a, b) => a.index - b.index);
78+
79+
allTokens.forEach(e => {
80+
console.log("< " + e.type + " > : \n" + e.token);
81+
console.log("----------------------------------");
82+
});
4683

47-
allTokens.sort( (a,b) => a.index - b.index );
84+
rw.saveFile( 'RESULTS/'+path.split('/')[1] , allTokens.map( e => "< " + e.type + " > : -" + e.token + '-' ).join('\n') );
4885

49-
allTokens.forEach( e => {
50-
console.log( '< '+e.type +' > : \n'+ e.token );
51-
console.log("----------------------------------");
86+
});
5287
});

Diff for: ‎operations.js

+5
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ module.exports = [
6363
name: 'MULTIPLY',
6464
reg: /\*/g
6565
},
66+
{
67+
name: 'DIV',
68+
reg: /\//g
69+
}
70+
,
6671
{
6772
name: 'LESSTHAN',
6873
reg: /\</g

Diff for: ‎reservedWords.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ function all(){
8888
},
8989
{
9090
name: 'STRING',
91-
reg: /string/g
91+
reg: /String/g
9292
},
9393
{
9494
name: 'FLOAT',
@@ -209,10 +209,6 @@ function all(){
209209
{
210210
name: 'TRY',
211211
reg: /try/g
212-
},
213-
{
214-
name: 'SYSTEM.OUT.PRINTLN',
215-
reg: /system\.out\.print/g
216212
}
217213
];
218214
}

Diff for: ‎rw.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const fs = require('fs');
2+
3+
function getFileContent(srcPath, callback) {
4+
fs.readFile(srcPath, 'utf8', function (err, data) {
5+
if (err) throw err;
6+
callback(data);
7+
}
8+
);
9+
}
10+
11+
function saveFile(savPath , data ) {
12+
fs.writeFile (savPath, data, function(err) {
13+
if (err) throw err;
14+
console.log('complete');
15+
});
16+
}
17+
18+
module.exports = {
19+
getFileContent,
20+
saveFile
21+
}

Diff for: ‎types.js

+29-25
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
11
module.exports = [
2-
{
3-
name: 'FLOAT_LITERAL',
4-
reg: /([ \*\/\-\+\=])(\d+\.\d+)/g
5-
},
6-
{
7-
name: 'INTEGRAL_LITERAL',
8-
reg: /([ \*\/\-\+\=]|^)(\d+)/g
9-
},
10-
{
11-
name: 'STRING_LITERAL',
12-
reg: /()(\"(\\\"|[^\"])*\")/g
13-
},
14-
{
15-
name: 'S_COMMENTS',
16-
reg: /()(\/\/.*\n?)/g
17-
},
18-
{
19-
name: 'M_COMMENTS',
20-
reg: /()(\/\*[^\*\/]*\*\/)/g
21-
},
22-
{
23-
name: 'A_CHAR',
24-
// we can allow \n chars
25-
reg: /()(\'(\\.|[^\'])?\')/g
26-
},
2+
{
3+
name: "M_COMMENTS",
4+
reg: /([^\"]\s*)(\/\*[^\*\/]*\*\/)(\s*[^\"])/g
5+
},
6+
{
7+
name: "S_COMMENTS",
8+
reg: /()(\/\/.*\n?)/g
9+
},
10+
{
11+
name: "SYSTEM.OUT.PRINTLN",
12+
reg: /System\.out\.println/g
13+
},
14+
{
15+
name: "STRING_LITERAL",
16+
reg: /()(\"(\\\"|[^\"])*\")/g
17+
},
18+
{
19+
name: "A_CHAR",
20+
// we can allow \n chars
21+
reg: /()(\'(\\.|[^\'])?\')/g
22+
},
23+
{
24+
name: "FLOAT_LITERAL",
25+
reg: /([ \*\/\-\+\=])(\d+\.\d+)/g
26+
},
27+
{
28+
name: "INTEGRAL_LITERAL",
29+
reg: /([ \*\/\-\+\=]|^)(\d+)/g
30+
},
2731
];

0 commit comments

Comments
 (0)
Please sign in to comment.