Skip to content
This repository has been archived by the owner on Sep 26, 2023. It is now read-only.

Commit

Permalink
Add support for custom rules
Browse files Browse the repository at this point in the history
Add rule to check keyword formatting
  • Loading branch information
j-denisb committed Nov 13, 2018
1 parent 718633a commit e816542
Show file tree
Hide file tree
Showing 8 changed files with 265 additions and 64 deletions.
11 changes: 6 additions & 5 deletions ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ function forloop(t) {
function foreach(t) {
return {
node: 'foreach',
var: t[4],
in: t[8],
statements: t[10].statements,
var: t[2],
in: t[6],
statements: t[7].statements,

tokens: xtok(t)
}
Expand All @@ -151,7 +151,8 @@ function whileloop(t) {
function print(t) {
return {
node: 'print',
items: t[1] // expressions and separataros
items: t[1], // expressions and separataros
tokens: xtok(t)
}
}

Expand Down Expand Up @@ -364,7 +365,7 @@ function dim(t) {
name: t[2],
// array of expressions
dimentions: t[6].expressions,
tokens: xtok[t]
tokens: xtok(t)
}
}
function expr_list(t) {
Expand Down
12 changes: 7 additions & 5 deletions brs.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,12 @@ var grammar = {
{"name": "end_for$ebnf$1", "symbols": ["end_for$ebnf$1$subexpression$1"], "postprocess": id},
{"name": "end_for$ebnf$1", "symbols": [], "postprocess": function(d) {return null;}},
{"name": "end_for", "symbols": [{"literal":"next"}, "end_for$ebnf$1"]},
{"name": "for_each", "symbols": [{"literal":"for"}, "_", {"literal":"each"}, "__", "IDENTIFIER", "__", {"literal":"in"}, "_", "rval", "statement_list", "end_for_each"], "postprocess": ast.foreach},
{"name": "for_each$subexpression$1", "symbols": [{"literal":"for"}, "__", {"literal":"each"}]},
{"name": "for_each$subexpression$1", "symbols": [{"literal":"foreach"}]},
{"name": "for_each", "symbols": ["for_each$subexpression$1", "__", "IDENTIFIER", "__", {"literal":"in"}, "_", "rval", "statement_list", "end_for_each"], "postprocess": ast.foreach},
{"name": "end_for_each", "symbols": [{"literal":"end"}, "__", {"literal":"for"}]},
{"name": "end_for_each", "symbols": [{"literal":"endfor"}]},
{"name": "end_for_each", "symbols": [{"literal":"next"}]},
{"name": "end_for_each", "symbols": [{"literal":"next"}], "postprocess": id},
{"name": "while_loop$subexpression$1", "symbols": [{"literal":"end"}, "__", {"literal":"while"}]},
{"name": "while_loop$subexpression$1", "symbols": [{"literal":"endwhile"}]},
{"name": "while_loop", "symbols": [{"literal":"while"}, "_", "EXPR", "statement_list", "while_loop$subexpression$1"], "postprocess": ast.while},
Expand All @@ -195,9 +197,9 @@ var grammar = {
{"name": "end_statement", "symbols": [{"literal":"end"}], "postprocess": ast.end},
{"name": "goto_label", "symbols": ["IDENTIFIER", "_", {"literal":":"}]},
{"name": "goto_statement", "symbols": [{"literal":"goto"}, "__", "IDENTIFIER"]},
{"name": "print_statement$subexpression$1", "symbols": [{"literal":"print"}]},
{"name": "print_statement$subexpression$1", "symbols": [{"literal":"?"}]},
{"name": "print_statement", "symbols": ["print_statement$subexpression$1", "print_items"], "postprocess": ast.print},
{"name": "print_statement", "symbols": ["print", "print_items"], "postprocess": ast.print},
{"name": "print", "symbols": [{"literal":"print"}], "postprocess": id},
{"name": "print", "symbols": [{"literal":"?"}], "postprocess": id},
{"name": "print_items$ebnf$1", "symbols": []},
{"name": "print_items$ebnf$1", "symbols": ["print_items$ebnf$1", "psep"], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}},
{"name": "print_items", "symbols": ["print_items$ebnf$1"], "postprocess": id},
Expand Down
10 changes: 7 additions & 3 deletions brs.ne
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,11 @@ for_loop ->
statement_list end_for {% ast.for %}
end_for -> "end" __ "for" | "endfor" | "next" (__ IDENTIFIER):?
# `endfor :` <- results in error, `next :` is ok :(

for_each ->
"for" _ "each" __ IDENTIFIER __ "in" _ rval statement_list end_for_each {% ast.foreach %}
end_for_each -> "end" __ "for" | "endfor" | "next"
("for" __ "each" | "foreach") __ IDENTIFIER __
"in" _ rval statement_list end_for_each {% ast.foreach %}
end_for_each -> "end" __ "for" | "endfor" | "next" {% id %}

while_loop ->
"while" _ EXPR statement_list ("end" __ "while" | "endwhile") {% ast.while %}
Expand All @@ -180,7 +182,9 @@ goto_label -> IDENTIFIER _ ":"

goto_statement -> "goto" __ IDENTIFIER

print_statement -> ("print" | "?") print_items {% ast.print %}
print_statement -> print print_items {% ast.print %}
print -> "print" {% id %}
| "?" {% id %}
print_items ->
psep:* {% id %}
| psep:* EXPR (_ PEXPR | pxsp EXPR):* psep:* {% ast.print_items %}
Expand Down
16 changes: 10 additions & 6 deletions brslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@ function traverse(node, callback, ctx) {
function traverseRule(node, rule, warnings) {
if (!node) return

if (node.node === rule.node) {
let l = rule.check(node)
if (l) warnings.push(
{ s: rule.level, msg: rule.message, loc: l }
)
if (!rule.node || node.node === rule.node) {
let warn = rule.check(node)
if (warn) {
if (Array.isArray(warn)) {
warnings.push(...warn)
} else {
warnings.push(warn)
}
}
}

for (const key in node) {
Expand Down Expand Up @@ -73,7 +77,7 @@ function unassignedVar(node, vars) {
}
if (node.node == 'id' && (node.accessors == null || node.accessors[0].node != 'call')) {
if (vars.indexOf(node.val.toLowerCase()) < 0 && globals.indexOf(node.val) < 0) {
warnings.push({ msg: 'Undefined variable \'' + node.val + '\'', loc: node.li.line+','+node.li.col, s: 1 })
warnings.push({ message: 'Undefined variable \'' + node.val + '\'', loc: node.li.line+','+node.li.col, level: 1 })
return
}
}
Expand Down
4 changes: 2 additions & 2 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ function showErrors(errors, file) {
function showWarnings(warnings) {
if (args.message == 'all') {
warnings.forEach( function (warning) {
if (warning.s <= parseInt(args.warning)) {
console.log(color.yellowBright(' Warning: ') + warning.msg + ' @' + warning.loc)
if (warning.level <= parseInt(args.warning)) {
console.log(color.yellowBright(' Warning: ') + warning.message + ' @' + warning.loc)
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "brslint",
"version": "0.1.7",
"version": "0.1.8",
"description": "BrightScript Lint",
"preferGlobal": true,
"bin": {
Expand Down
Loading

0 comments on commit e816542

Please sign in to comment.