Skip to content

Commit

Permalink
Merge pull request #26 from teknologi-umum/feat/lua
Browse files Browse the repository at this point in the history
feat: language lua
  • Loading branch information
Reinaldy Rafli committed Aug 23, 2021
2 parents 1bc4ceb + c426bfd commit 60864b1
Show file tree
Hide file tree
Showing 12 changed files with 388 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import { Go } from './languages/go';
import { HTML } from './languages/html';
import { Java } from './languages/java';
import { Javascript } from './languages/javascript';
import { Julia } from './languages/julia';
import { Lua } from './languages/lua';
import { PHP } from './languages/php';
import { Python } from './languages/python';
import { Ruby } from './languages/ruby';
import { Julia } from './languages/julia';
import { Rust } from './languages/rust';
import { SQL } from './languages/sql';
import { nearTop, getPoints } from './points';
Expand Down Expand Up @@ -46,6 +47,7 @@ const languages: Record<string, LanguagePattern[]> = {
Java,
Javascript,
Julia,
Lua,
PHP,
Python,
Ruby,
Expand Down
2 changes: 2 additions & 0 deletions src/languages/c.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@ export const C: LanguagePattern[] = [
/(new|this\s)?(List|IEnumerable)<(sbyte|byte|short|ushort|int|uint|long|ulong|float|double|decimal|bool|char|string)>/,
type: 'not',
},
// Avoiding Lua confusion
{ pattern: /local\s(function|\w+)?/, type: 'not' },
];
2 changes: 2 additions & 0 deletions src/languages/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ export const Javascript: LanguagePattern[] = [
{ pattern: /(using\s)?System(\..*)?(;)?/, type: 'not' },
{ pattern: /(func|fn)\s/, type: 'not' },
{ pattern: /(begin|end)\n/, type: 'not' },
// Avoiding Lua confusion
{ pattern: /local\s(function|(\w+)\s=)/, type: 'not' },
];
2 changes: 2 additions & 0 deletions src/languages/julia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ export const Julia: LanguagePattern[] = [
{ pattern: /def\s+\w+\s*(\(.+\))?\s*\n/, type: 'not' },
{ pattern: /puts\s+("|').+("|')/, type: 'not' },
{ pattern: /class\s/, type: 'not' },
// Avoiding Lua confusion
{ pattern: /local\s(function|\w+)/, type: 'not' },
];
63 changes: 63 additions & 0 deletions src/languages/lua.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import type { LanguagePattern } from '../types';

export const Lua: LanguagePattern[] = [
// multiline string
{ pattern: /(\[\[.*\]\])/, type: 'constant.string' },
// local definition
{ pattern: /local\s([a-zA-Z0-9_]+)(\s*=)?/, type: 'keyword.variable' },
// function definition
{ pattern: /(local\s)?function\s*([a-zA-Z0-9_]*)?\(\)/, type: 'keyword.function' },
// for loop
{ pattern: /for\s+([a-zA-Z]+)\s*=\s*([a-zA-Z0-9_]+),\s*([a-zA-Z0-9_]+)\s+do/, type: 'keyword.control' },
// while loop
{ pattern: /while\s(.*)\sdo/, type: 'keyword.control' },
// keywords
{
pattern:
/\s+(and|break|do|else|elseif|end|false|function|if|in|not|or|local|repeat|return|then|true|until|pairs|ipairs|in|yield)/,
type: 'keyword.other',
},
{ pattern: /nil/, type: 'constant.null' },
// length operator
{ pattern: /#([a-zA-Z_{}]+)/, type: 'keyword.operator' },
// metatables
{ pattern: /((get|set)metatable|raw(get|set|equal))\(.*\)/, type: 'keyword.other' },
// metamethods
{ pattern: /__(index|newindex|call|sub|mul|div|mod|pow|unm|eq|le|lt)/, type: 'keyword.other' },
// method invocation
{ pattern: /(\(.+\)|([a-zA-Z_]+)):([a-zA-Z_])\(.*\)/, type: 'keyword.other' },
// array-like table
{ pattern: /{\s*(\S+)((,|;)\s*\S+)*\s*}/, type: 'constant.array' },
// map-like table
{ pattern: /{\s*(\S+\s*=\s*\S+)((,|;)\s*\S+\s*=\s*\S+)*\s*}/, type: 'constant.dictionary' },
// builtin math methods
{ pattern: /math\.(.*)\([0-9]*\)/, type: 'macro' },
// builtin table methods
{ pattern: /table\.(.*)\(.*\)/, type: 'macro' },
// builtin io methods
{ pattern: /io\.(.*)\(.*\)/, type: 'macro' },
// builtin functions
{ pattern: /(require|dofile)\((.*)\)/, type: 'meta.import' },
{ pattern: /(pcall|xpcall|unpack|pack|coroutine)/, type: 'keyword.other' },
// comments
{ pattern: /--(\[\[)?.*/, type: 'comment.line' },
// rest arguments
{ pattern: /\.\.\./, type: 'keyword.other' },

// invalid comments
{ pattern: /(\/\/|\/\*)/, type: 'not' },
// avoid confusion with C
{ pattern: /(#(include|define)|printf|\s+int\s+)/, type: 'not' },
// avoid confusion with javascript
{ pattern: /\s+(let|const|var)\s+/, type: 'not' },
// avoid confusion with PHP & Python
{ pattern: /\s+(echo|die|\$(.*))\s+/, type: 'not' },
// avoid confusion with Python
{ pattern: /(def|len|from|import)/, type: 'not' },
// avoid confusion with SQL
{ pattern: /(SELECT|FROM|INSERT|ALTER)/, type: 'not' },
// avoid confusion with Ruby
{ pattern: /(puts)/, type: 'not' },
// avoid confusion Julia
{ pattern: /(([a-zA-Z0-9]+)::([a-zA-Z0-9]+)|using|(.*)!\(.*\)|(\|\|))/, type: 'not' },
];
2 changes: 2 additions & 0 deletions src/languages/php.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ export const PHP: LanguagePattern[] = [
{ pattern: /(^|\s)(var|char|long|int|float|double)\s+\w+\s*=?/, type: 'not' },
// Javascript variable declaration
{ pattern: /(var|const|let)\s+\w+\s*=?/, type: 'not' },
// Avoiding Lua confusion
{ pattern: /local\s(function|\w+)/, type: 'not' },
];
3 changes: 3 additions & 0 deletions src/languages/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@ export const Python: LanguagePattern[] = [
{ pattern: /print((\s*\(.+\))|\s+.+)/, type: 'keyword.print' },
// &&/|| operators
{ pattern: /(&{2}|\|{2})/, type: 'not' },
// avoiding lua
{ pattern: /elseif/, type: 'not' },
{ pattern: /local\s(function|\w+)?\s=\s/, type: 'not' },
];
3 changes: 3 additions & 0 deletions src/languages/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ export const SQL: LanguagePattern[] = [
{ pattern: /(BIT|TINYINT|SMALLINT|MEDIUMINT|INT|INTEGER|BIGINT|DOUBLE)\([0-9]+\)/, type: 'constant.type' },
{ pattern: /(TINYBLOB|TINYTEXT|MEDIUMTEXT|MEDIUMBLOB|LONGTEXT|LONGBLOB)/, type: 'constant.type' },
{ pattern: /(BOOLEAN|BOOL|DATE|YEAR)/, type: 'constant.type' },
// Avoiding Lua
{ pattern: /local\s(function|\w+)?\s=\s/, type: 'not' },
{ pattern: /(require|dofile)\((.*)\)/, type: 'not' },
];
3 changes: 2 additions & 1 deletion tests/cpp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,19 @@ test('hello world', () => {
C: 0,
'C++': 5,
CSS: 0,
'C#': 0,
Go: 0,
HTML: 0,
Java: 0,
Javascript: 0,
Julia: 2,
Lua: 2,
PHP: 0,
Python: 0,
Ruby: 0,
Rust: 0,
SQL: 0,
Unknown: 1,
'C#': 0,
});
});

Expand Down
1 change: 1 addition & 0 deletions tests/cs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ test('hello world', () => {
Java: -40,
Javascript: -40,
Julia: 5,
Lua: -20,
PHP: 0,
Python: 0,
Ruby: 0,
Expand Down
1 change: 1 addition & 0 deletions tests/large.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,7 @@ test('large input', () => {
Java: -832,
Javascript: -452,
Julia: 24,
Lua: -1820,
PHP: -318,
Python: -140,
Ruby: 0,
Expand Down
Loading

0 comments on commit 60864b1

Please sign in to comment.