openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 8f213b6c51839f58dd0ab8187b0c5017503a777b
parent d4fdafce11f3f92140dfd8835dd828a32863fcde
Author: Jan Dankert <develop@jandankert.de>
Date:   Sun, 26 Jun 2022 12:51:07 +0200

New: DSL can be controlled by flags; support for error messages; support for negativ numbers.

Diffstat:
Mmodules/cms/generator/TemplateGenerator.class.php | 2+-
Mmodules/cms/generator/ValueGenerator.class.php | 4++--
Mmodules/dsl/DslLexer.class.php | 2+-
Mmodules/dsl/DslToken.class.php | 4++++
Mmodules/dsl/executor/DslInterpreter.class.php | 37++++++++++++++++++++++++++++++++-----
Amodules/dsl/standard/Script.class.php | 42++++++++++++++++++++++++++++++++++++++++++
6 files changed, 82 insertions(+), 9 deletions(-)

diff --git a/modules/cms/generator/TemplateGenerator.class.php b/modules/cms/generator/TemplateGenerator.class.php @@ -132,7 +132,7 @@ class TemplateGenerator $templateParser = new DslTemplate(); $templateParser->parseTemplate($src); if ($templateParser->tagsFound) { - $executor = new DslInterpreter(); + $executor = new DslInterpreter( DslInterpreter::FLAG_THROW_ERROR ); $executor->addContext([ 'console' => new DslConsole(), 'http' => new DslHttp(), diff --git a/modules/cms/generator/ValueGenerator.class.php b/modules/cms/generator/ValueGenerator.class.php @@ -797,7 +797,7 @@ class ValueGenerator extends BaseGenerator break; case self::CODE_SCRIPT: - $executor = new DslInterpreter(); + $executor = new DslInterpreter(DslInterpreter::FLAG_THROW_ERROR ); $executor->addContext( [ 'console' => new DslConsole(), 'http' => new DslHttp(), @@ -1184,7 +1184,7 @@ class ValueGenerator extends BaseGenerator */ protected function filterValue( $inhalt, $code) { - $executor = new DslInterpreter(); + $executor = new DslInterpreter(DslInterpreter::FLAG_THROW_ERROR ); $executor->addContext( [ 'page' => new DslObject( (new BaseObject($this->context->pageContext->objectId))->load() ), diff --git a/modules/dsl/DslLexer.class.php b/modules/dsl/DslLexer.class.php @@ -175,7 +175,7 @@ class DslLexer } // Numbers - if ( $char >= '0' && $char <= '9') { + if ( $char >= '0' && $char <= '9' || $char == '-') { $value = $char; while( true ) { $char = array_shift( $chars ); diff --git a/modules/dsl/DslToken.class.php b/modules/dsl/DslToken.class.php @@ -44,4 +44,8 @@ class DslToken $this->value = $value; } + public function __toString() + { + return '#'.$this->lineNumber.':'.$this->type.':"'.$this->value.'"'; + } } \ No newline at end of file diff --git a/modules/dsl/executor/DslInterpreter.class.php b/modules/dsl/executor/DslInterpreter.class.php @@ -5,6 +5,7 @@ namespace dsl\executor; use dsl\DslAstParser; use dsl\DslException; use dsl\DslLexer; +use dsl\standard\Script; use dsl\standard\StandardArray; use dsl\standard\StandardDate; use dsl\standard\StandardMath; @@ -24,9 +25,17 @@ class DslInterpreter * @var Write */ private $writer; + private $flags; - public function __construct() + const FLAG_SHOW_ERROR = 1; + const FLAG_SHOW_TRACE = 2; + const FLAG_THROW_ERROR = 4; + const FLAG_DEBUG = 8; + + public function __construct( $flags = self::FLAG_SHOW_ERROR ) { + $this->flags = $flags; + // Standard-Globals $this->addContext( [ 'Math' => new StandardMath(), @@ -60,11 +69,29 @@ class DslInterpreter $token = $lexer->tokenize( $code ); // Step 2: Creating a syntax tree (abstract syntax tree, AST). - $parser = new DslAstParser(); - $parser->parse( $token ); + try { + + $parser = new DslAstParser(); + $parser->parse( $token ); + + //if ( $this->flags & self::FLAG_DEBUG ) + // it has no security impact, so lets do it always. + $this->addContext( + [ 'Script' => new Script( $token,$parser->rootStatement ) ] + ); - // Step 3: Executing the syntax tree. - return $parser->execute( $this->context ); + // Step 3: Executing the syntax tree. + return $parser->execute( $this->context ); + } catch ( \Exception $e ) { + if ( $this->flags & self::FLAG_SHOW_ERROR ) { + if ( $this->flags & self::FLAG_SHOW_TRACE ) + $this->writer->buffer .= $e->__toString(); + else + $this->writer->buffer .= $e->getMessage(); + } + if ( $this->flags & self::FLAG_THROW_ERROR ) + throw $e; + } } diff --git a/modules/dsl/standard/Script.class.php b/modules/dsl/standard/Script.class.php @@ -0,0 +1,41 @@ +<?php + +namespace dsl\standard; + +use dsl\ast\DslStatement; +use dsl\context\DslObject; +use dsl\DslToken; + +class Script implements DslObject +{ + /** + * @var DslToken[] + */ + private $tokens; + + /** + * @var DslStatement + */ + private $ast; + + /** + * @param $tokens DslToken[] + * @param $ast DslStatement + */ + public function __construct($tokens, $ast ) + { + $this->tokens = $tokens; + $this->ast = $ast; + } + + public function getToken() + { + return implode("\n",$this->tokens); + } + + + public function getSyntaxTree() + { + return print_r($this->ast,true); + } +} +\ No newline at end of file