Skip to content

Commit

Permalink
Tweaks
Browse files Browse the repository at this point in the history
Signed-off-by: Anand Krishnamoorthi <[email protected]>
  • Loading branch information
anakrish committed Feb 6, 2024
1 parent 404e71b commit bb77d6d
Showing 1 changed file with 193 additions and 179 deletions.
372 changes: 193 additions & 179 deletions index.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@

layout: home
---

<html>

<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Regorus Playground</title>
<link
rel="stylesheet"
href="/style.css"
/>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Regorus Playground</title>
<link rel="stylesheet" href="style.css" />
</head>

<body>
<h1>Regorus Playground</h1>
<div class="pane">
Expand All @@ -39,188 +37,204 @@ href="/style.css"
</div>
</div>
</div>

<script>
var require = { paths: { vs: "/node_modules/monaco-editor/min/vs" } };
var require = { paths: { vs: "node_modules/monaco-editor/min/vs" } };


</script>

<script src="/node_modules/monaco-editor/min/vs/loader.js"></script>
<script src="/node_modules/monaco-editor/min/vs/editor/editor.main.nls.js"></script>
<script src="/node_modules/monaco-editor/min/vs/editor/editor.main.js"></script>

<script type="module">



import init, { Engine } from "{{'pkg/regorus.js'|relative_url}}";

function loadFile(filePath) {
filePath = `/${filePath}`
var result = null;
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", filePath, false);
xmlhttp.send();
if (xmlhttp.status==200) {
result = xmlhttp.responseText;
}
return result;
}


monaco.languages.register( { id : 'Rego'})

let rego_language = {
// defaultToken: 'invalid',

keywords: [
'as',
'contains',
'default',
'else',
'every',
'false',
'if',
'import',
'in',
'not',
'null',
'package',
'set(',
'some',
'true',
'with',
],

brackets: [
['{','}','delimiter.curly'],
['[',']','delimiter.square'],
['(',')','delimiter.parenthesis']
],

operators: [
'+', '-', '*', '/', '%',
'&', '|',
',', ';', '.',
':',
'<', '<=', '=', '==', '>', '>=',
'!', '!=',
],

// we include these common regular expressions
symbols: /[=><!~?:&|+\-*\/\^%]+/,

tokenizer: {
root: [
[/[a-zA-Z'_\?\\][\w'\?\\]*/, { cases: {'@keywords': 'keyword',
'@default' : 'identifier' }}],

[':=', 'keyword'],

[/"[^\\"]*"/, 'string'],
[/`[^\\`]*`/, 'string'],

// delimiters and operators
[/[{}()\[\]]/, '@brackets'],
[/[<>](?!@symbols)/, '@brackets'],


[/@symbols/, { cases: { '@operators': 'delimiter',
'@default' : '' } } ],

// numbers
[/[0-9_]*\.[0-9_]+([eE][\-+]?\d+)?[fFdD]?/, 'number.float'],
[/[0-9_]+/, 'number'],

// delimiter: after number because of .\d floats
[/[;,.]/, 'delimiter'],
// whitespace
{ include: '@whitespace' },

],

whitespace: [
[/[ \t\r\n]+/, 'white'],
[/#.*$/, 'comment'],
],

}
};


monaco.languages.setMonarchTokensProvider('Rego', rego_language)


let separator = "\n###POLICY###\n"
let framework_rego = 'package play'
var rego_editor = monaco.editor.create(document.getElementById('Rego'), {
value: framework_rego,
language: 'Rego',
minimap: { enabled: false },
});

var input_editor = monaco.editor.create(document.getElementById('Input'), {
value: '{}',
language: 'json',
minimap: { enabled: false },
});

var data_editor = monaco.editor.create(document.getElementById('Data'), {
value: '{}',
language: 'json',
minimap: { enabled: false },
});
var results_editor = monaco.editor.create(document.getElementById('Output'), {
value: '{}',
language: 'json',
readOnly: true,
minimap: { enabled: false },
})

rego_editor.setValue(loadFile("examples/example.rego"))
input_editor.setValue(loadFile("examples/input.json"))
data_editor.setValue("{}")
document.getElementById('Data').style.display = 'none'

function eval_query() {
let policy = rego_editor.getValue();
let files = policy.split(separator)

try {
var startTime = new Date();
var engine = new Engine();
for (var i =0; i < files.length; ++i) {
engine.add_policy("policy.rego", files[i])
}

engine.set_input(input_editor.getValue());
engine.add_data(data_editor.getValue());
let parse_time = new Date() - startTime;

let results = engine.eval_query("data");
var elapsed = new Date() - startTime;
let output = `// Evaluation took ${elapsed} milliseconds. parse = ${parse_time}, eval = ${elapsed - parse_time}\n${results}`;
results_editor.setValue(output)
}
catch (error) {
results_editor.setValue(error)
}
}
async function run() {
await init()
document.getElementById('Eval').onclick = eval_query;
document.getElementById('SelectInput').onclick = function() {
document.getElementById('Data').style.display = 'none'
document.getElementById('Input').style.display = 'inline'
}
document.getElementById('SelectData').onclick = function() {
document.getElementById('Input').style.display = 'none'
document.getElementById('Data').style.display = 'inline'
}
}
run();

<script type="module">



import init, { Engine } from "/pkg/regorus.js";

function loadFile(filePath) {
filePath = `./${filePath}`
var result = null;
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", filePath, false);
xmlhttp.send();
if (xmlhttp.status == 200) {
result = xmlhttp.responseText;
}
return result;
}


monaco.languages.register({ id: 'Rego' })

let rego_language = {
// defaultToken: 'invalid',

keywords: [
'as',
'contains',
'default',
'else',
'every',
'false',
'if',
'import',
'in',
'not',
'null',
'package',
'set(',
'some',
'true',
'with',
],


brackets: [
['{', '}', 'delimiter.curly'],
['[', ']', 'delimiter.square'],
['(', ')', 'delimiter.parenthesis']
],

operators: [
'+', '-', '*', '/', '%',
'&', '|',
',', ';', '.',
':',
'<', '<=', '=', '==', '>', '>=',
'!', '!=',
],

// we include these common regular expressions
symbols: /[=><!~?:&|+\-*\/\^%]+/,

tokenizer: {
root: [
[/[a-zA-Z'_\?\\][\w'\?\\]*/, {
cases: {
'@keywords': 'keyword',
'@default': 'identifier'
}
}],

[':=', 'keyword'],

[/"[^\\"]*"/, 'string'],
[/`[^\\`]*`/, 'string'],

// delimiters and operators
[/[{}()\[\]]/, '@brackets'],
[/[<>](?!@symbols)/, '@brackets'],


[/@symbols/, {
cases: {
'@operators': 'delimiter',
'@default': ''
}
}],

// numbers
[/[0-9_]*\.[0-9_]+([eE][\-+]?\d+)?[fFdD]?/, 'number.float'],
[/[0-9_]+/, 'number'],

// delimiter: after number because of .\d floats
[/[;,.]/, 'delimiter'],
// whitespace
{ include: '@whitespace' },

],

whitespace: [
[/[ \t\r\n]+/, 'white'],
[/#.*$/, 'comment'],
],

}
};


monaco.languages.setMonarchTokensProvider('Rego', rego_language)


let separator = "\n###POLICY###\n"
let framework_rego = 'package play'
var rego_editor = monaco.editor.create(document.getElementById('Rego'), {
value: framework_rego,
language: 'Rego',
minimap: { enabled: false },
});

var input_editor = monaco.editor.create(document.getElementById('Input'), {
value: '{}',
language: 'json',
minimap: { enabled: false },
});

var data_editor = monaco.editor.create(document.getElementById('Data'), {
value: '{}',
language: 'json',
minimap: { enabled: false },
});

var results_editor = monaco.editor.create(document.getElementById('Output'), {
value: '{}',
language: 'json',
readOnly: true,
minimap: { enabled: false },
})

rego_editor.setValue(loadFile("examples/example.rego"))
input_editor.setValue(loadFile("examples/input.json"))
data_editor.setValue("{}")
document.getElementById('Data').style.display = 'none'

function eval_query() {
let policy = rego_editor.getValue();
let files = policy.split(separator)

try {
var startTime = new Date();
var engine = new Engine();
for (var i = 0; i < files.length; ++i) {
engine.add_policy("policy.rego", files[i])
}

engine.set_input(input_editor.getValue());
engine.add_data(data_editor.getValue());
let parse_time = new Date() - startTime;

let results = engine.eval_query("data");
var elapsed = new Date() - startTime;
let output = `// Evaluation took ${elapsed} milliseconds. parse = ${parse_time}, eval = ${elapsed - parse_time}\n${results}`;
results_editor.setValue(output)
}
catch (error) {
results_editor.setValue(error)
}
}
async function run() {
await init()
document.getElementById('Eval').onclick = eval_query;
document.getElementById('SelectInput').onclick = function () {
document.getElementById('Data').style.display = 'none'
document.getElementById('Input').style.display = 'inline'
}
document.getElementById('SelectData').onclick = function () {
document.getElementById('Input').style.display = 'none'
document.getElementById('Data').style.display = 'inline'
}
}
run();
</script>


</body>

</html>

0 comments on commit bb77d6d

Please sign in to comment.