简体   繁体   中英

Flutter Dart parse advanced expressions

I have string with a lot of expressions, and must return true or false. I tried do it using this library: https://pub.dev/packages/expression_language/example but have some problems with strings.

This is sample of code from library:

import 'package:expression_language/expression_language.dart';

void main() {
var input = '\"Hello 1 + 1 equals: \" +  (1 + 1)'
  var expressionGrammarDefinition = ExpressionGrammarParser({});
  var parser = expressionGrammarDefinition.build();
  var result = parser.parse(input);
  var expression = result.value as Expression;
  var value = expression.evaluate();
  print(value);
}

output is: Hello 1 + 1 equals: 2

But my expressions are more complicated, for example:

input = '(!\"Dog\" == \"Cat\")';
input = '(1+1) || (\"Dog\" == \"Dog\")';
input = '(1+1) + (\"2\" * 2)';

I can parse this using expression_language library? Maybe exist better solution?

expression_language is built with petitparser , which is a very flexible general purpose parser. If you learn petitparser, you'll be able to build anything! (You may never write another regex in Dart as well.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM