Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
wvanheemstra committed Nov 9, 2024
1 parent 30a6608 commit fd268c3
Show file tree
Hide file tree
Showing 10 changed files with 1,199 additions and 989 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ Blockly.Blocks['ast_AuthorDef'] = {
};

Blockly.Python['ast_AuthorDef'] = function (block) { // Name
console.debug("Blockly.Python['ast_AuthorDef']");
let name = Blockly.Python.variableDB_.getName(block.getFieldValue('NAME'), Blockly.Variables.NAME_TYPE);
// Decorators
let decorators = new Array(block.decorators_);
Expand Down Expand Up @@ -105,6 +106,7 @@ Blockly.Python['ast_AuthorDef'] = function (block) { // Name
};

BlockMirrorTextToBlocks.prototype['ast_AuthorDef'] = function (node, parent) {
console.debug("BlockMirrorTextToBlocks['ast_AuthorDef']");
let name = node.name;
let bases = node.bases;
let keywords = node.keywords;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ Blockly.Blocks['ast_ClassDef'] = {
};

Blockly.Python['ast_ClassDef'] = function (block) {
console.debug("Blockly.Python['ast_ClassDef']");
// Name
let name = Blockly.Python.variableDB_.getName(block.getFieldValue('NAME'), Blockly.Variables.NAME_TYPE);
// Decorators
Expand Down Expand Up @@ -107,6 +108,7 @@ Blockly.Python['ast_ClassDef'] = function (block) {
;

BlockMirrorTextToBlocks.prototype['ast_ClassDef'] = function (node, parent) {
console.debug("BlockMirrorTextToBlocks['ast_ClassDef']");
let name = node.name;
let bases = node.bases;
let keywords = node.keywords;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ BlockMirrorTextToBlocks.prototype.isSingleChar = function (text) {
BlockMirrorTextToBlocks.prototype.isDocString = function (node, parent) {
return (parent._astname === 'Expr' &&
parent._parent &&
['FunctionDef', 'ClassDef'].indexOf(parent._parent._astname) !== -1 &&
['FunctionDef', 'ClassDef', 'AuthorDef'].indexOf(parent._parent._astname) !== -1 &&
parent._parent.body[0] === parent);
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// ALL IMPLEMENTED AS ORIGINAL

function BlockMirrorTextToBlocks(blockMirror) {
console.debug('BlockMirrorTextToBlocks');
this.blockMirror = blockMirror;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ TOOLBOX_CATEGORY.THREAT_MODELS = {
};
TOOLBOX_CATEGORY.AUTHOR = {
name: 'Author', colour: 'AUTHOR', blocks: [
"def author(): ___",
"name: my name",
"homepage: my url",
"author:",
" name: my name",
" homepage: my url",
]
};
TOOLBOX_CATEGORY.DATA_ASSETS = {
Expand Down
64 changes: 61 additions & 3 deletions threagile-builder/src/threagile_builder/static/lib/skulpt/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ function astForDecorators (c, n) {
}

function ast_for_decorated (c, n) {
/* decorated: decorators (classdef | funcdef | async_funcdef) */
/* decorated: decorators (authordef | classdef | funcdef | async_funcdef) */
var thing = null;
var decorator_seq = null;

Expand All @@ -525,12 +525,15 @@ function ast_for_decorated (c, n) {
decorator_seq = astForDecorators(c, CHILD(n, 0));
Sk.asserts.assert(TYPE(CHILD(n, 1)) == SYM.funcdef ||
TYPE(CHILD(n, 1)) == SYM.async_funcdef ||
TYPE(CHILD(n, 1)) == SYM.classdef);
TYPE(CHILD(n, 1)) == SYM.classdef ||
TYPE(CHILD(n, 1)) == SYM.authordef);

if (TYPE(CHILD(n, 1)) == SYM.funcdef) {
thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
} else if (TYPE(CHILD(n, 1)) == SYM.classdef) {
thing = astForClassdef(c, CHILD(n, 1), decorator_seq);
} else if (TYPE(CHILD(n, 1)) == SYM.authordef) {
thing = astForAuthordef(c, CHILD(n, 1), decorator_seq);
} else if (TYPE(CHILD(n, 1)) == SYM.async_funcdef) {
thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
}
Expand Down Expand Up @@ -1576,6 +1579,58 @@ function astForClassdef (c, n, decoratorSeq) {
n.end_lineno, n.end_col_offset);
}




function astForAuthordef (c, n, decoratorSeq) {
/* authordef: 'author' NAME ['(' arglist ')'] ':' suite */
var classname;
var call;
var s;

REQ(n, SYM.authordef);

if (NCH(n) == 4) { /* class NAME ':' suite */
s = astForSuite(c, CHILD(n, 3));
classname = new_identifier(CHILD(n, 1).value);
forbiddenCheck(c, CHILD(n,3), classname, n.lineno);

return new Sk.astnodes.AuthorDef(classname, [], [], s, decoratorSeq,
/*TODO docstring*/null, LINENO(n), n.col_offset,
n.end_lineno, n.end_col_offset);
}

if (TYPE(CHILD(n, 3)) === TOK.T_RPAR) { /* class NAME '(' ')' ':' suite */
s = astForSuite(c, CHILD(n, 5));
classname = new_identifier(CHILD(n, 1).value);
forbiddenCheck(c, CHILD(n, 3), classname, CHILD(n, 3).lineno);
return new Sk.astnodes.AuthorDef(classname, [], [], s, decoratorSeq,
/*TODO docstring*/null, LINENO(n), n.col_offset,
n.end_lineno, n.end_col_offset);
}

/* class NAME '(' arglist ')' ':' suite */
/* build up a fake Call node so we can extract its pieces */
{
var dummy_name;
var dummy;
dummy_name = new_identifier(CHILD(n, 1));
dummy = new Sk.astnodes.Name(dummy_name, Sk.astnodes.Load, LINENO(n), n.col_offset,
n.end_lineno, n.end_col_offset);
call = ast_for_call(c, CHILD(n, 3), dummy, false);
}
s = astForSuite(c, CHILD(n, 6));
classname = new_identifier(CHILD(n, 1).value);
forbiddenCheck(c, CHILD(n,1), classname, CHILD(n,1).lineno);

return new Sk.astnodes.AuthorDef(classname, call.args, call.keywords, s,
decoratorSeq, /*TODO docstring*/null, LINENO(n), n.col_offset,
n.end_lineno, n.end_col_offset);
}




function astForLambdef (c, n) {
/* lambdef: 'lambda' [varargslist] ':' test */
var args;
Expand Down Expand Up @@ -3082,7 +3137,7 @@ function astForStmt (c, n) {
}
else {
/* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
| funcdef | classdef | decorated | async_stmt
| funcdef | classdef | authordef | decorated | async_stmt
*/
ch = CHILD(n, 0);
REQ(n, SYM.compound_stmt);
Expand All @@ -3101,6 +3156,8 @@ function astForStmt (c, n) {
return ast_for_funcdef(c, ch, []);
case SYM.classdef:
return astForClassdef(c, ch, []);
case SYM.authordef:
return astForAuthordef(c, ch, []);
case SYM.decorated:
return ast_for_decorated(c, ch);
case SYM.async_stmt:
Expand Down Expand Up @@ -3240,6 +3297,7 @@ Sk.INHERITANCE_MAP = {
'stmt': [Sk.astnodes.FunctionDef,
Sk.astnodes.AsyncFunctionDef,
Sk.astnodes.ClassDef,
Sk.astnodes.AuthorDef,
Sk.astnodes.Return,
Sk.astnodes.Delete,
Sk.astnodes.Assign,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,34 @@ Sk.astnodes.ClassDef = function ClassDef(/* {identifier} */ name, /* {asdl_seq
return this;
}

/** @constructor */
Sk.astnodes.AuthorDef = function AuthorDef(/* {identifier} */ name, /* {asdl_seq
*} */ bases, /* {asdl_seq *} */
keywords, /* {asdl_seq *} */
body, /* {asdl_seq *} */
decorator_list, /* {string} */
docstring, /* {int} */ lineno, /*
{int} */ col_offset, /* {int} */
end_lineno, /* {int} */
end_col_offset)
{
Sk.asserts.assert(lineno !== null && lineno !== undefined);
Sk.asserts.assert(col_offset !== null && col_offset !== undefined);
Sk.asserts.assert(end_lineno !== null && end_lineno !== undefined);
Sk.asserts.assert(end_col_offset !== null && end_col_offset !== undefined);
this.name = name;
this.bases = bases;
this.keywords = keywords;
this.body = body;
this.decorator_list = decorator_list;
this.docstring = docstring;
this.lineno = lineno;
this.col_offset = col_offset;
this.end_lineno = end_lineno;
this.end_col_offset = end_col_offset;
return this;
}

/** @constructor */
Sk.astnodes.Return = function Return(/* {expr_ty} */ value, /* {int} */ lineno,
/* {int} */ col_offset, /* {int} */
Expand Down Expand Up @@ -1444,6 +1472,15 @@ Sk.astnodes.ClassDef.prototype._fields = [
"decorator_list", function(n) { return n.decorator_list; },
"docstring", function(n) { return n.docstring; }
];
Sk.astnodes.AuthorDef.prototype._astname = "AuthorDef";
Sk.astnodes.AuthorDef.prototype._fields = [
"name", function(n) { return n.name; },
"bases", function(n) { return n.bases; },
"keywords", function(n) { return n.keywords; },
"body", function(n) { return n.body; },
"decorator_list", function(n) { return n.decorator_list; },
"docstring", function(n) { return n.docstring; }
];
Sk.astnodes.Return.prototype._astname = "Return";
Sk.astnodes.Return.prototype._fields = [
"value", function(n) { return n.value; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ sym:
xor_expr: 340,
yield_arg: 341,
yield_expr: 342,
yield_stmt: 343},
yield_stmt: 343,
authordef: 344},
number2symbol:
{256: 'single_input',
257: 'and_expr',
Expand Down Expand Up @@ -225,7 +226,8 @@ number2symbol:
340: 'xor_expr',
341: 'yield_arg',
342: 'yield_expr',
343: 'yield_stmt'},
343: 'yield_stmt',
344: 'authordef'},
dfas:
{256: [[[[1, 1], [2, 2], [3, 1]], [[0, 1]], [[3, 1]]],
{3: 1,
Expand Down Expand Up @@ -1704,7 +1706,8 @@ labels:
[338, null],
[257, null],
[32, null],
[341, null]],
[341, null]
[344, 'author']],
keywords:
{'False': 23,
'null': 13,
Expand Down Expand Up @@ -1740,7 +1743,8 @@ keywords:
'try': 21,
'while': 9,
'with': 31,
'yield': 8},
'yield': 8,
'author': 344},
tokens:
{0: 112,
1: 28,
Expand Down
Loading

0 comments on commit fd268c3

Please sign in to comment.