Skip to content

Commit

Permalink
added syntax validator
Browse files Browse the repository at this point in the history
  • Loading branch information
kaganrua committed Aug 20, 2024
1 parent 203c5c6 commit 1c25619
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 22 deletions.
9 changes: 9 additions & 0 deletions frontend/package-lock.json

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

1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"pino-caller": "^3.4.0",
"pino-pretty": "^11.2.1",
"pyodide": "^0.26.2",
"python-shell": "^5.0.0",
"rfdc": "^1.3.1",
"s3-sync-client": "^4.3.1",
"source-map-support": "^0.5.21",
Expand Down
22 changes: 13 additions & 9 deletions frontend/resources/compileComputation.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import {parser} from '@lezer/python';
import { TreeCursor } from '@lezer/common';

Check failure on line 2 in frontend/resources/compileComputation.mjs

View workflow job for this annotation

GitHub Actions / lint (20.x)

'TreeCursor' is defined but never used
import { readFileSync } from 'fs';

Check failure on line 3 in frontend/resources/compileComputation.mjs

View workflow job for this annotation

GitHub Actions / lint (20.x)

'readFileSync' is defined but never used
import {PythonShell} from 'python-shell'



async function validateSource(source) {
await PythonShell.checkSyntax(source)
}

export function compileComputationFunction(source) {
const result = extractIO(source)
Expand All @@ -12,16 +16,20 @@ export function compileComputationFunction(source) {
}
return result
}
function extractIO(source) {
const tree = parser.parse(source)



async function extractIO(source) {
await validateSource(source)
const tree = parser.parse(source);
const cursor = tree.cursor()

const functionInfo = {}
while(cursor.next()) {
if(cursor.node.name === 'FunctionDefinition') {
cursor.firstChild()
cursor.nextSibling()
const functionName = source.slice(cursor.node.from, cursor.node.to)
console.log(cursor.node.name)
if(functionName === 'compute') {

cursor.nextSibling();
Expand All @@ -45,23 +53,19 @@ function extractIO(source) {

const outputs = {}
while(cursor.nextSibling()){
console.log(cursor.node.name)
if(cursor.node.name === 'Body'){
cursor.firstChild()
while(cursor.nextSibling()){
console.log(cursor.node.name)
console.log(cursor.node)


if(cursor.node.name === 'ReturnStatement'){
cursor.firstChild()
cursor.nextSibling()
if(cursor.node.name === 'DictionaryExpression') {
cursor.firstChild()
cursor.nextSibling()
// console.log(cursor.node.name)
while(cursor.node.name !== '}'){
cursor.nextSibling()
// console.log(cursor.node.name)
if(cursor.node.name === 'VariableName'){
const outputName = source.slice(cursor.node.from, cursor.node.to)
outputs[outputName] = {
Expand Down
18 changes: 7 additions & 11 deletions frontend/resources/compileComputation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,18 @@
import json
import sys

with open("/Users/kaganrua/Desktop/zetaforge-github/zetaforge/frontend/core/blocks/new-python/computations.py") as f:
source = f.read()
# with open("/Users/kaganrua/Desktop/zetaforge-github/zetaforge/frontend/core/blocks/new-python/computations.py") as f:
# source = f.read()



def extract_io(source):
tree = ast.parse(source)
print(tree)
function_info = {}
for node in ast.walk(tree):
print("NODE", node)
if isinstance(node, ast.FunctionDef) and node.name == 'compute':
# Extract docstring
docstring = ast.get_docstring(node)
print(docstring)
function_info['description'] = docstring

# Extract inputs
Expand Down Expand Up @@ -54,10 +51,9 @@ def extract_io(source):
break

return function_info
io = extract_io(source)
print(json.dumps(io))

# if __name__ == '__main__':
# source = sys.stdin.read()
# io = extract_io(source)
# print(json.dumps(io))

if __name__ == '__main__':
source = sys.stdin.read()
io = extract_io(source)
print(json.dumps(io))
11 changes: 9 additions & 2 deletions frontend/server/blockSerialization.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,15 @@ export async function compileComputation(blockPath) {
);
}


const io = compileComputationFunction(source)
let io;
try{
io = await compileComputationFunction(source)
} catch(err) {
throw new ServerError(
`Failed to compile your python script. Please check your syntax. ${err.message}`,
HttpStatus.INTERNAL_SERVER_ERROR
)
}
return io;
}

Expand Down

0 comments on commit 1c25619

Please sign in to comment.