This repository has been archived by the owner on Aug 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6f2c274
commit 6886f57
Showing
5 changed files
with
329 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
export const grammar = `/* description: Parses and executes mathematical expressions. */ | ||
/* lexical grammar */ | ||
%lex | ||
%% | ||
\s+ /* skip whitespace */ | ||
[0-9]+("."[0-9]+)?\b return 'NUMBER'; | ||
"*" return '*'; | ||
"/" return '/'; | ||
"-" return '-'; | ||
"+" return '+'; | ||
"^" return '^'; | ||
"!" return '!'; | ||
"%" return '%'; | ||
"(" return '('; | ||
")" return ')'; | ||
"PI" return 'PI'; | ||
"E" return 'E'; | ||
<<EOF>> return 'EOF'; | ||
. return 'INVALID'; | ||
/lex | ||
/* operator associations and precedence */ | ||
%left '+' '-' | ||
%left '*' '/' | ||
%left '^' | ||
%right '!' | ||
%right '%' | ||
%left UMINUS | ||
%token INVALID | ||
%start expressions | ||
%% /* language grammar */ | ||
expressions | ||
: e EOF | ||
{ typeof console !== 'undefined' ? console.log($1) : print($1); | ||
return $1; } | ||
; | ||
e | ||
: e '+' e | ||
{$$ = $1 + $3;} | ||
| e '-' e | ||
{$$ = $1 - $3;} | ||
| e '*' e | ||
{$$ = $1 * $3;} | ||
| e '/' e | ||
{$$ = $1 / $3;} | ||
| e '^' e | ||
{$$ = Math.pow($1, $3);} | ||
| e '!' | ||
{{ | ||
$$ = (function fact(n) { | ||
return n == 0 ? 1 : fact(n - 1) * n; | ||
})($1); | ||
}} | ||
| e '%' | ||
{$$ = $1 / 100;} | ||
| '-' e %prec UMINUS | ||
{$$ = -$2;} | ||
| '(' e ')' | ||
{$$ = $2;} | ||
| NUMBER | ||
{$$ = Number(yytext);} | ||
| E | ||
{$$ = Math.E;} | ||
| PI | ||
{$$ = Math.PI;} | ||
; | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import React, { useState } from 'react'; | ||
import parser from './parser' | ||
import { TypeAheadSelect } from 'patternfly-react'; | ||
|
||
const options = [ | ||
{ value: 'Alabama' }, | ||
{ value: 'Florida' }, | ||
{ value: 'New Jersey' }, | ||
{ value: 'New Mexico' }, | ||
{ value: 'New York' }, | ||
{ value: 'North Carolina' } | ||
]; | ||
|
||
const parse = (text, callback) => { | ||
try { | ||
return { output: JSON.stringify(parser.parse(text)), isValid: true}; | ||
} | ||
catch(err) { | ||
console.log(text); | ||
console.log(err); | ||
// console.log('Expected:', err.hash.expected, 'Token:', err.hash.token, "Value:", err.hash.value, err.hash.text, "Line:", err.stack ); | ||
return { output: err.message, isValid: false}; | ||
} | ||
} | ||
|
||
const isValidDiv = (isValid) => (( | ||
isValid | ||
? <p style={{backgroundColor: 'lightGreen'}}>{isValid ? 'Expression je validni' : 'Expression JE validni'}</p> | ||
: <p style={{backgroundColor: 'red'}}>{isValid ? 'Expression je validni' : 'Expression NENI validni'}</p> | ||
)); | ||
|
||
export default function ExpressionEditor() { | ||
// Declare a new state variable, which we'll call "count" | ||
const [inputText, setInputText] = useState(""); | ||
const [parsedInput, setParsedInput] = useState(""); | ||
const [isValid, setIsValid] = useState(true); | ||
const parseObj = parse(inputText); | ||
|
||
return ( | ||
<div> | ||
<TypeAheadSelect | ||
options={[]} | ||
onInputChange = {(x) => setInputText(x)} | ||
emptyLabel={null} | ||
isValid={isValid} | ||
isInvalid={!isValid} | ||
> | ||
|
||
</TypeAheadSelect> | ||
<p>{inputText}</p> | ||
{isValidDiv(parseObj.isValid)} | ||
<div>{parseObj.output}</div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
const grammar = `/* description: Parses and executes mathematical expressions. */ | ||
/* lexical grammar */ | ||
%lex | ||
%% | ||
\\s+ /* NO return 'SPACE' */ | ||
":" return ':' | ||
"." return '.' | ||
"(" return '(' | ||
")" return ')' | ||
["'].*?["'] return 'TEXT_VALUE' | ||
'"' return '"' | ||
"=" return '=' | ||
">" return '>' | ||
"<" return '<' | ||
"AND" return 'AND' | ||
"OR" return 'OR' | ||
"NOT" return 'NOT' | ||
"FIELD" return 'FIELD' | ||
"FIND" return 'FIND' | ||
"TAGS" return 'TAG' | ||
"COUNT OF" return 'COUNTOF' | ||
"REGISTRY" return 'REGKEY' | ||
"CONTAINS" return 'CONTAINS' | ||
"IS" return 'IS' | ||
"STARTS WITH" return 'STARTS_WITH' | ||
"EMPTY" return 'EMPTY' | ||
"INCLUDES" return 'INCLUDES' | ||
"FROM" return 'FROM' | ||
"THROUGH" return 'THROUGH' | ||
"REGULAR EXPRESSION MATCHES" return 'REGEXP_MATCH' | ||
"CHECK ALL" return 'CHECKALL' | ||
"CHECK ANY" return 'CHECKANY' | ||
"CHECK COUNT" return 'CHECKCOUNT' | ||
[a-zA-Z_0-9]+ return 'LITERAL' | ||
<<EOF>> return 'EOF' | ||
/lex | ||
%start expression | ||
%left 'OR' | ||
%left 'AND' | ||
%left 'NOT' | ||
%% /* language grammar */ | ||
expression | ||
: e EOF | ||
{ typeof console !== 'undefined' ? console.log($1) : print($1); | ||
return $1; } | ||
; | ||
e | ||
: field_expression | ||
| tag_expression | ||
| count_expression | ||
| find_expression | ||
| e OR e | ||
{{$$={type: 'expressionOperator', value: $2, left: $1, right: $3 };}} | ||
| e AND e | ||
{{$$={type: 'expressionOperator', value: $2, left: $1, right: $3 };}} | ||
| NOT e | ||
{{$$={type: 'expressionOperator', value: "NOT", next: $2 };}} | ||
| '(' e ')' | ||
{{$$={type: 'parentheses', value: $2, left: $1, right: $3 };}} | ||
| e error | ||
| error | ||
{ yyerrok } | ||
; | ||
field_expression | ||
: FIELD ':' entity '.' field operator_and_value | ||
{{$$={type: $1, entity: $3, field: $5, operator: $6, value: $6.value};}} | ||
| entity '.' field operator_and_value | ||
{{$$={type: 'field', entity: $1, field: $3, operator: $4, value: $4.value};}} | ||
; | ||
/* | ||
tags: VM.category value | ||
tags: VM.category: value | ||
tags: VM.category = value | ||
*/ | ||
tag_expression | ||
: TAG ':' entity '.' category tag_operator value | ||
{{$$={type: $1, entity: $3, category: $5, value: $7};}} | ||
; | ||
tag_operator | ||
: | ':' | CONTAINS | '=' | ||
; | ||
count_expression | ||
: COUNTOF ':' entity operator_and_value | ||
{{$$={type: $1, entity: $3, operator: $4, value: $4.value};}} | ||
; | ||
find_expression | ||
: FIND ':' entity '.' field operator_and_value check | ||
{{$$={type: $1, entity: $3, field: $5, operator: $6, value: $6.value, check: $7 };}} | ||
; | ||
reg_expression | ||
: REGKEY ':' key ':' reg_value ':' operator | ||
{{$$={type: $1, entity: $3, field: $5, operator: $7 };}} | ||
; | ||
type | ||
: LITERAL | ||
{{$$={type: 'expType', value: $1.trim()};}} | ||
; | ||
entity | ||
: LITERAL | ||
{{$$={type: 'entity', value: $1.trim()};}} | ||
| TEXT_VALUE | ||
{{$$={type: 'entity', value: $1.slice(1,$1.length-1)};}} | ||
| entity '.' LITERAL | ||
{{$$={type: 'relation', next: $1, value: $3.trim()};}} | ||
| entity '.' TEXT_VALUE | ||
{{$$={type: 'relation', next: $1, value: $3.slice(1,$3.length-1)};}} | ||
; | ||
field | ||
: LITERAL | ||
{{$$={type: 'field', value: $1.trim()};}} | ||
| TEXT_VALUE | ||
{{$$={type: 'field', value: $1.slice(1,$1.length-1)};}} | ||
; | ||
category | ||
: LITERAL | ||
{{$$={type: 'category', value: $1.trim()};}} | ||
| TEXT_VALUE | ||
{{$$={type: 'category', value: $1.slice(1,$1.length-1)};}} | ||
; | ||
operator_and_value | ||
: CONTAINS value | ||
{{$$={type: 'operator', operator: $1.trim(), value: $2};}} | ||
| '=' value | ||
{{$$={type: 'operator', operator: $1.trim(), value: $2};}} | ||
| '>' '=' value | ||
{{$$={type: 'operator', operator: '>=', value: $3};}} | ||
| '<' '=' value | ||
{{$$={type: 'operator', operator: '<=', value: $3};}} | ||
| INCLUDES value | ||
{{$$={type: 'operator', operator: $1.trim(), value: $2};}} | ||
| 'IS' 'NOT' 'EMPTY' | ||
{{$$={type: 'operator', operator: 'IS_NOT_EMPTY'};}} | ||
| 'IS' value | ||
{{$$={type: 'operator', operator: 'IS', value: $2};}} | ||
| 'STARTS_WITH' value | ||
{{$$={type: 'operator', operator: $1.trim(), value: $2};}} | ||
| 'FROM' value 'THROUGH' value | ||
{{$$={type: 'operator', operator: $1.trim(), value: [$2, $4]};}} | ||
| 'REGEXP_MATCH' value | ||
{{$$={type: 'operator', operator: $1, value: $2};}} | ||
; | ||
value | ||
: LITERAL | ||
{{$$={type: 'value', value: $1.trim()};}} | ||
| TEXT_VALUE | ||
{{$$={type: 'value', value: $1.slice(1,$1.length-1)};}} | ||
; | ||
check | ||
: CHECKALL ':' field operator_and_value | ||
{{$$={type: 'check_all', field: $3, operator: $4, check_value: $4.value};}} | ||
| CHECKANY ':' field operator_and_value | ||
{{$$={type: 'check_any', field: $3, operator: $4, check_value: $4.value};}} | ||
| CHECKCOUNT ':' operator_and_value | ||
{{$$={type: 'check_count', operator: $3, check_value: $3.value};}} | ||
; | ||
`; | ||
|
||
export default grammar; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import grammar from './grammar.jison.js' | ||
// var Parser = require("jison-gho").Parser; | ||
import { Parser } from 'jison-gho' | ||
// import { Parser } from '/home/panspagetka/react-ui-components/node_modules/jison-gho/dist/jison-es6.js' | ||
// require('fs') | ||
|
||
// import grammar_jison from '../../grammar_jison' | ||
// `grammar` can also be a string that uses jison's grammar format | ||
const parser = new Parser(grammar); | ||
|
||
export default parser; |
10 changes: 10 additions & 0 deletions
10
src/expression-editor/stories/expression-editor.stories.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import React from 'react'; | ||
import ExpressionEditor from '../expression-editor' | ||
|
||
import { storiesOf } from '@storybook/react'; | ||
|
||
storiesOf('Expression Editor', module) | ||
.add('Expression Editor', () => | ||
( | ||
<ExpressionEditor/> | ||
)); |