mirror of
https://github.com/amyinspace/MagicSetEditor2.git
synced 2026-06-10 04:57:00 -04:00
Closure operator now behaves as default argument operator, documentation.
git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@965 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
@@ -12,3 +12,41 @@ MSE uses a custom scripting language to add complicated behaviour to [[type:fiel
|
||||
See also:
|
||||
* [[type:index|Data types used]]
|
||||
* [[fun:index|Built in functions]]
|
||||
|
||||
--Syntax index--
|
||||
| @123@ [[type:int|A literal number]]
|
||||
| @"stuff"@ [[type:string|A literal string]]
|
||||
| @[a,b,c]@ [[type:list|A literal list]]
|
||||
| @[a:b, c:d]@ [[type:map|A literal map]]
|
||||
| @{ expr }@ [[script:variables#Functions|Function definition]]
|
||||
| @fun(a:b, c:d)@ [[script:variables#Functions|Function call]]
|
||||
| @fun(value)@ [[script:variables#Functions|Function call with '@input@' argument]]
|
||||
| @fun@@(a:b)@ [[script:variables#Default arguments|Default arguments]]
|
||||
| @a.b@ [[script:operators|Property 'b' of 'a']]
|
||||
| @a[b]@ [[script:operators|Property 'value of b' of 'a']]
|
||||
| @-a@ [[script:operators|Negation]]
|
||||
| @a + b@ [[script:operators|Addition / concatenation]]
|
||||
| @a - b@ [[script:operators|Subtraction]]
|
||||
| @a * b@ [[script:operators|Multiplication]]
|
||||
| @a / b@ [[script:operators|Floating point division]]
|
||||
| @a div b@ [[script:operators|Integer division]]
|
||||
| @a mod b@ [[script:operators|Remainder]]
|
||||
| @not a@ [[type:boolean|Boolean not]]
|
||||
| @a and b@ [[type:boolean|Boolean conjunction]]
|
||||
| @a or b@ [[type:boolean|Boolean disjunction]]
|
||||
| @a xor b@ [[type:boolean|Boolean xor]]
|
||||
| @a == b@ [[script:operators|Comparison for equality]]
|
||||
| @a != b@ [[script:operators|Comparison for inequality]]
|
||||
| @a < b@ [[script:operators|Comparison]]
|
||||
| @a > b@ [[script:operators|Comparison]]
|
||||
| @a <= b@ [[script:operators|Comparison]]
|
||||
| @a >= b@ [[script:operators|Comparison]]
|
||||
| @a or else b@ Use @a@ unless it is an error, then use @b@ instead.
|
||||
| @min(a,b,c,...)@ [[script:operators|Smallest of the values]]
|
||||
| @max(a,b,c,...)@ [[script:operators|Largest of the values]]
|
||||
| @rgb(r,g,b)@ [[type:color|A color value]]
|
||||
| @rgba(r,g,b)@ [[type:color|A color value with transparency]]
|
||||
| @if x then y@ [[script:control structures|Conditional expresion]]
|
||||
| @if x then y else z@ [[script:control structures|Conditional expresion]]
|
||||
| @for x in list do y@ [[script:control structures|Loop over elements in a list]]
|
||||
| @for x from a to b do y@ [[script:control structures|Loop over numbers from a to b]]
|
||||
|
||||
@@ -74,4 +74,17 @@ This can be done by first making a copy, and calling that:
|
||||
> to_upper("xyz") == "upper case: XYZ"
|
||||
Note that @real_to_upper@ is called without extra parameters, the @input@ variable is still set from the outer call to the new @to_upper@ itself.
|
||||
|
||||
--Default arguments--
|
||||
It is possible to declare default arguments for functions using the @@@@ operator.
|
||||
> function := { "argument was: " + arg }@(arg:"default")
|
||||
If this function is called without the @arg@ argument, then the default value @default@ is used instead.
|
||||
For example:
|
||||
> function() == "argument was: default"
|
||||
> function(arg: "something else") == "argument was: something else"
|
||||
For determining whether the argument is set only explicit arguments count, not everything in scope, so
|
||||
> arg := "something else"
|
||||
> function() == "argument was: default"
|
||||
|
||||
Defaults are evaluated at the time the @@@@ operator is evaluated, so they can be used to simulate static scoping.
|
||||
|
||||
<div style="text-align:right;">next: <a href="control_structures">Control structures →</a></div>
|
||||
|
||||
@@ -177,20 +177,20 @@ void KeywordReminderTextValue::highlight(const String& code, const vector<Script
|
||||
}
|
||||
|
||||
bool KeywordReminderTextValue::checkScript(const ScriptP& script) {
|
||||
Context& ctx = set.cards.empty() ? set.getContext() : set.getContext(set.cards.front());
|
||||
size_t scope = ctx.openScope();
|
||||
try {
|
||||
Context& ctx = set.cards.empty() ? set.getContext() : set.getContext(set.cards.front());
|
||||
LocalScope scope(ctx);
|
||||
for (size_t i = 0 ; i < keyword.parameters.size() ; ++i) {
|
||||
String param = String(_("param")) << (int)(i+1);
|
||||
ctx.setVariable(param, to_script(param));
|
||||
}
|
||||
script->eval(ctx);
|
||||
errors.clear();
|
||||
return true;
|
||||
} catch (const Error& e) {
|
||||
errors = e.what();
|
||||
return false;
|
||||
}
|
||||
ctx.closeScope(scope);
|
||||
return errors.empty();
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : Changing keywords : mode
|
||||
|
||||
+77
-24
@@ -12,6 +12,8 @@
|
||||
#include <util/error.hpp>
|
||||
#include <iostream>
|
||||
|
||||
DECLARE_TYPEOF_COLLECTION(pair<Variable COMMA ScriptValueP>);
|
||||
|
||||
// ----------------------------------------------------------------------------- : Context
|
||||
|
||||
Context::Context()
|
||||
@@ -34,6 +36,10 @@ void instrQuaternary(QuaternaryInstructionType i, ScriptValueP& a, const ScriptV
|
||||
|
||||
|
||||
ScriptValueP Context::eval(const Script& script, bool useScope) {
|
||||
if (level > 500) {
|
||||
throw ScriptError(_("Stack overflow"));
|
||||
}
|
||||
|
||||
size_t stack_size = stack.size();
|
||||
size_t scope = useScope ? openScope() : 0;
|
||||
try {
|
||||
@@ -140,7 +146,7 @@ ScriptValueP Context::eval(const Script& script, bool useScope) {
|
||||
|
||||
// Closure object
|
||||
case I_CLOSURE: {
|
||||
makeClosure(i.data);
|
||||
makeClosure(i.data, instr);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -221,6 +227,10 @@ ScriptValueP Context::getVariable(Variable var) {
|
||||
if (variables[var].value) return variables[var].value;
|
||||
throw ScriptError(_("Variable not set: ") + variable_to_string(var));
|
||||
}
|
||||
int Context::getVariableScope(Variable var) {
|
||||
if (variables[var].value) return level - variables[var].level;
|
||||
else return -1;
|
||||
}
|
||||
|
||||
|
||||
size_t Context::openScope() {
|
||||
@@ -259,6 +269,62 @@ void instrUnary (UnaryInstructionType i, ScriptValueP& a) {
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : Function composition
|
||||
|
||||
/// Composition of two functions
|
||||
class ScriptCompose : public ScriptValue {
|
||||
public:
|
||||
ScriptCompose(ScriptValueP a, ScriptValueP b) : a(a), b(b) {}
|
||||
|
||||
virtual ScriptType type() const { return SCRIPT_FUNCTION; }
|
||||
virtual String typeName() const { return _("function composition"); }
|
||||
virtual ScriptValueP eval(Context& ctx) const {
|
||||
ctx.setVariable(SCRIPT_VAR_input, a->eval(ctx));
|
||||
return b->eval(ctx);
|
||||
}
|
||||
virtual ScriptValueP dependencies(Context& ctx, const Dependency& dep) const {
|
||||
ctx.setVariable(SCRIPT_VAR_input, a->dependencies(ctx, dep));
|
||||
return b->dependencies(ctx, dep);
|
||||
}
|
||||
private:
|
||||
ScriptValueP a,b;
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------- : Closures
|
||||
|
||||
/// A closure around a function
|
||||
class ScriptClosure : public ScriptValue {
|
||||
public:
|
||||
ScriptClosure(ScriptValueP fun) : fun(fun) {}
|
||||
|
||||
/// Add a binding
|
||||
void bind(Variable v, const ScriptValueP& value) {
|
||||
bindings.push_back(make_pair(v,value));
|
||||
}
|
||||
/// Apply the bindings
|
||||
void applyBindings(Context& ctx) const {
|
||||
FOR_EACH_CONST(b, bindings) {
|
||||
if (ctx.getVariableScope(b.first) != 0) {
|
||||
ctx.setVariable(b.first, b.second);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
virtual ScriptType type() const { return SCRIPT_FUNCTION; }
|
||||
virtual String typeName() const { return _("function closure"); }
|
||||
virtual ScriptValueP eval(Context& ctx) const {
|
||||
applyBindings(ctx);
|
||||
return fun->eval(ctx);
|
||||
}
|
||||
virtual ScriptValueP dependencies(Context& ctx, const Dependency& dep) const {
|
||||
applyBindings(ctx);
|
||||
return fun->dependencies(ctx, dep);
|
||||
}
|
||||
private:
|
||||
ScriptValueP fun;
|
||||
vector<pair<Variable,ScriptValueP> > bindings;
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------- : Simple instructions : binary
|
||||
|
||||
// operator on ints
|
||||
@@ -284,24 +350,6 @@ void instrUnary (UnaryInstructionType i, ScriptValueP& a) {
|
||||
} \
|
||||
break
|
||||
|
||||
/// Composition of two functions
|
||||
class ScriptCompose : public ScriptValue {
|
||||
public:
|
||||
ScriptCompose(ScriptValueP a, ScriptValueP b) : a(a), b(b) {}
|
||||
|
||||
virtual ScriptType type() const { return SCRIPT_FUNCTION; }
|
||||
virtual String typeName() const { return _("function composition"); }
|
||||
virtual ScriptValueP eval(Context& ctx) const {
|
||||
ctx.setVariable(SCRIPT_VAR_input, a->eval(ctx));
|
||||
return b->eval(ctx);
|
||||
}
|
||||
virtual ScriptValueP dependencies(Context& ctx, const Dependency& dep) const {
|
||||
ctx.setVariable(SCRIPT_VAR_input, a->dependencies(ctx, dep));
|
||||
return b->dependencies(ctx, dep);
|
||||
}
|
||||
private:
|
||||
ScriptValueP a,b;
|
||||
};
|
||||
|
||||
void instrBinary (BinaryInstructionType i, ScriptValueP& a, const ScriptValueP& b) {
|
||||
switch (i) {
|
||||
@@ -408,9 +456,14 @@ void Context::makeObject(size_t n) {
|
||||
stack.push_back(ret);
|
||||
}
|
||||
|
||||
void Context::makeClosure(size_t n) {
|
||||
//intrusive_ptr<ScriptClosure> ret(new ScriptClosure());
|
||||
// TODO
|
||||
//stack.push_back(ret);
|
||||
throw InternalError(_("TODO: makeClosure"));
|
||||
void Context::makeClosure(size_t n, const Instruction*& instr) {
|
||||
intrusive_ptr<ScriptClosure> closure(new ScriptClosure(stack[stack.size() - n - 1]));
|
||||
for (size_t j = 0 ; j < n ; ++j) {
|
||||
closure->bind((Variable)instr[n - j - 1].data, stack.back());
|
||||
stack.pop_back();
|
||||
}
|
||||
// skip arguments
|
||||
instr += n;
|
||||
// set value
|
||||
stack.back() = closure;
|
||||
}
|
||||
|
||||
@@ -62,12 +62,20 @@ class Context {
|
||||
ScriptValueP getVariable(Variable var);
|
||||
/// Get the value of a variable, returns ScriptValue() if it is not set
|
||||
inline ScriptValueP getVariableOpt(Variable var) { return variables[var].value; }
|
||||
/// In what scope was the variable set?
|
||||
/** Returns 0 for the current scope and >0 for outer scopes.
|
||||
* Returns -1 if the varible is not set
|
||||
*/
|
||||
int getVariableScope(Variable var);
|
||||
|
||||
private:
|
||||
|
||||
/// Open a new scope
|
||||
/** returns the number of shadowed binding before that scope */
|
||||
size_t openScope();
|
||||
/// Close a scope, must be passed a value from openScope
|
||||
void closeScope(size_t scope);
|
||||
friend class LocalScope;
|
||||
|
||||
public:// public for FOR_EACH
|
||||
/// Record of a variable
|
||||
@@ -102,7 +110,7 @@ class Context {
|
||||
/// Make an object with n elements, popping 2n values from the stack, and push it onto the stack
|
||||
void makeObject(size_t n);
|
||||
/// Make a closure with n arguments
|
||||
void makeClosure(size_t n);
|
||||
void makeClosure(size_t n, const Instruction*& instr);
|
||||
};
|
||||
|
||||
/// A class that creates a local scope
|
||||
|
||||
@@ -250,9 +250,9 @@ ScriptValueP Context::dependencies(const Dependency& dep, const Script& script)
|
||||
break;
|
||||
}
|
||||
|
||||
// Closure object
|
||||
// Closure object (as normal)
|
||||
case I_CLOSURE: {
|
||||
makeClosure(i.data);
|
||||
makeClosure(i.data, instr);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
+29
-25
@@ -104,8 +104,8 @@ class TokenIterator {
|
||||
vector<ScriptParseError>& errors;
|
||||
/// Add an error message
|
||||
void add_error(const String& message);
|
||||
/// Expected some token instead of what was found
|
||||
void expected(const String& exp);
|
||||
/// Expected some token instead of what was found, possibly a matching opening bracket is known
|
||||
void expected(const String& exp, const Token* opening = nullptr);
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------- : Characters
|
||||
@@ -293,25 +293,28 @@ void TokenIterator::readStringToken() {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void TokenIterator::add_error(const String& message) {
|
||||
if (!errors.empty() && errors.back().start == pos) return; // already an error here
|
||||
// find line number
|
||||
int line_number(size_t pos, const String& input) {
|
||||
int line = 1;
|
||||
for (size_t i = 0 ; i < input.size() && i < pos ; ++i) {
|
||||
if (input.GetChar(i) == _('\n')) line++;
|
||||
}
|
||||
errors.push_back(ScriptParseError(pos, line, filename, message));
|
||||
return line;
|
||||
}
|
||||
void TokenIterator::expected(const String& expected) {
|
||||
|
||||
void TokenIterator::add_error(const String& message) {
|
||||
if (!errors.empty() && errors.back().start == pos) return; // already an error here
|
||||
// add error message
|
||||
errors.push_back(ScriptParseError(pos, line_number(pos,input), filename, message));
|
||||
}
|
||||
void TokenIterator::expected(const String& expected, const Token* opening) {
|
||||
size_t error_pos = peek(0).pos;
|
||||
if (!errors.empty() && errors.back().start == pos) return; // already an error here
|
||||
// find line number
|
||||
int line = 1;
|
||||
for (size_t i = 0 ; i < input.size() && i < error_pos ; ++i) {
|
||||
if (input.GetChar(i) == _('\n')) line++;
|
||||
// add error message
|
||||
if (opening) {
|
||||
errors.push_back(ScriptParseError(opening->pos, error_pos, line_number(opening->pos,input), filename, opening->value, expected, peek(0).value));
|
||||
} else {
|
||||
errors.push_back(ScriptParseError(error_pos, line_number(error_pos,input), filename, expected, peek(0).value));
|
||||
}
|
||||
errors.push_back(ScriptParseError(error_pos, line, filename, expected, peek(0).value));
|
||||
}
|
||||
|
||||
|
||||
@@ -385,12 +388,12 @@ ScriptP parse(const String& s, Packaged* package, bool string_mode) {
|
||||
|
||||
|
||||
// Expect a token, adds an error if it is not found
|
||||
bool expectToken(TokenIterator& input, const Char* expect, const Char* name_in_error = nullptr) {
|
||||
bool expectToken(TokenIterator& input, const Char* expect, const Token* opening = nullptr, const Char* name_in_error = nullptr) {
|
||||
Token token = input.read();
|
||||
if (token == expect) {
|
||||
return true;
|
||||
} else {
|
||||
input.expected(name_in_error ? name_in_error : expect);
|
||||
input.expected(name_in_error ? name_in_error : expect, opening);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -398,16 +401,16 @@ bool expectToken(TokenIterator& input, const Char* expect, const Char* name_in_e
|
||||
void parseExpr(TokenIterator& input, Script& script, Precedence minPrec) {
|
||||
// usually loop only once, unless we encounter newlines
|
||||
while (true) {
|
||||
const Token& token = input.read();
|
||||
Token token = input.read();
|
||||
if (token == _("(")) {
|
||||
// Parentheses = grouping for precedence of expressions
|
||||
parseOper(input, script, PREC_ALL);
|
||||
expectToken(input, _(")"));
|
||||
expectToken(input, _(")"), &token);
|
||||
} else if (token == _("{")) {
|
||||
// {} = function block. Parse a new Script
|
||||
intrusive_ptr<Script> subScript(new Script);
|
||||
parseOper(input, *subScript, PREC_ALL);
|
||||
expectToken(input, _("}"));
|
||||
expectToken(input, _("}"), &token);
|
||||
script.addInstruction(I_PUSH_CONST, subScript);
|
||||
} else if (token == _("[")) {
|
||||
// [] = list or map literal
|
||||
@@ -432,7 +435,7 @@ void parseExpr(TokenIterator& input, Script& script, Precedence minPrec) {
|
||||
t = input.peek();
|
||||
}
|
||||
}
|
||||
expectToken(input, _("]"));
|
||||
expectToken(input, _("]"), &token);
|
||||
script.addInstruction(I_MAKE_OBJECT, count);
|
||||
} else if (minPrec <= PREC_UNARY && token == _("-")) {
|
||||
parseOper(input, script, PREC_UNARY, I_UNARY, I_NEGATE); // unary negation
|
||||
@@ -538,7 +541,7 @@ void parseExpr(TokenIterator& input, Script& script, Precedence minPrec) {
|
||||
parseOper(input, script, PREC_ALL); // second, third, etc.
|
||||
script.addInstruction(I_BINARY, op);
|
||||
}
|
||||
expectToken(input, _(")"));
|
||||
expectToken(input, _(")"), &token);
|
||||
} else {
|
||||
// variable
|
||||
Variable var = string_to_variable(token.value);
|
||||
@@ -572,7 +575,7 @@ void parseOper(TokenIterator& input, Script& script, Precedence minPrec, Instruc
|
||||
// EBNF: expr = expr | expr oper expr
|
||||
// without left recursion: expr = expr (oper expr)*
|
||||
while (true) {
|
||||
const Token& token = input.read();
|
||||
Token token = input.read();
|
||||
if (token != TOK_OPER && token != TOK_NAME && token!=TOK_LPAREN &&
|
||||
!((token == TOK_STRING || token == TOK_INT || token == TOK_DOUBLE) && minPrec <= PREC_NEWLINE && token.newline)) {
|
||||
// not an operator-like token
|
||||
@@ -646,11 +649,12 @@ void parseOper(TokenIterator& input, Script& script, Precedence minPrec, Instruc
|
||||
} else {
|
||||
script.addInstruction(I_BINARY, I_MEMBER);
|
||||
}
|
||||
expectToken(input, _("]"));
|
||||
expectToken(input, _("]"), &token);
|
||||
} else if (minPrec <= PREC_FUN && token==_("(")) {
|
||||
// function call, read arguments
|
||||
vector<Variable> arguments;
|
||||
parseCallArguments(input, script, arguments);
|
||||
expectToken(input, _(")"), &token);
|
||||
// generate instruction
|
||||
script.addInstruction(I_CALL, (unsigned int)arguments.size());
|
||||
FOR_EACH(arg,arguments) {
|
||||
@@ -658,9 +662,10 @@ void parseOper(TokenIterator& input, Script& script, Precedence minPrec, Instruc
|
||||
}
|
||||
} else if (minPrec <= PREC_FUN && token==_("@")) {
|
||||
// closure call, read arguments
|
||||
expectToken(input, _("("));
|
||||
vector<Variable> arguments;
|
||||
expectToken(input, _("("));
|
||||
parseCallArguments(input, script, arguments);
|
||||
expectToken(input, _(")"), &token);
|
||||
// generate instruction
|
||||
script.addInstruction(I_CLOSURE, (unsigned int)arguments.size());
|
||||
FOR_EACH(arg,arguments) {
|
||||
@@ -676,7 +681,7 @@ void parseOper(TokenIterator& input, Script& script, Precedence minPrec, Instruc
|
||||
} else {
|
||||
parseOper(input, script, PREC_ALL, I_BINARY, I_ADD); // e
|
||||
}
|
||||
if (expectToken(input, _("}\""), _("}"))) {
|
||||
if (expectToken(input, _("}\""), &token, _("}"))) {
|
||||
parseOper(input, script, PREC_NONE); // y
|
||||
// optimize: e + "" -> e
|
||||
i = script.getInstructions().back();
|
||||
@@ -724,5 +729,4 @@ void parseCallArguments(TokenIterator& input, Script& script, vector<Variable>&
|
||||
t = input.peek();
|
||||
}
|
||||
}
|
||||
expectToken(input, _(")"));
|
||||
}
|
||||
|
||||
@@ -33,6 +33,10 @@ ScriptParseError::ScriptParseError(size_t pos, int line, const String& filename,
|
||||
: ParseError(_("Expected '") + exp + _("' instead of '") + found + _("'"))
|
||||
, start(pos), end(pos + found.size()), line(line), filename(filename)
|
||||
{}
|
||||
ScriptParseError::ScriptParseError(size_t pos1, size_t pos2, int line, const String& filename, const String& open, const String& close, const String& found)
|
||||
: ParseError(_("Expected closing '") + close + _("' for this '") + open + _("' instead of '") + found + _("'"))
|
||||
, start(pos1), end(pos2 + found.size()), line(line), filename(filename)
|
||||
{}
|
||||
String ScriptParseError::what() const {
|
||||
return String(_("(")) << (int)start << _("): ") << Error::what();
|
||||
}
|
||||
|
||||
@@ -83,6 +83,7 @@ class ScriptParseError : public ParseError {
|
||||
public:
|
||||
ScriptParseError(size_t pos, int line, const String& filename, const String& str);
|
||||
ScriptParseError(size_t pos, int line, const String& filename, const String& expected, const String& found);
|
||||
ScriptParseError(size_t pos1, size_t pos2, int line, const String& filename, const String& open, const String& close, const String& found);
|
||||
/// Position of the error
|
||||
size_t start, end;
|
||||
/// Line number of the error (the first line is 1)
|
||||
|
||||
@@ -256,14 +256,15 @@ function autoformat__table($rows) {
|
||||
function autoformat__line($line) {
|
||||
$line = preg_replace("/'''(.*?)'''/", "<strong>\\1</strong>", $line);
|
||||
$line = preg_replace("/''(.*?)''/", "<em>\\1</em>", $line);
|
||||
$line = preg_replace_callback("/@(.*?)@/", "autoformat__code", $line);
|
||||
$line = preg_replace_callback("/@(([^@]|@@)*)@/", "autoformat__code", $line);
|
||||
$line = preg_replace_callback("/\[\[(.*?)\|(.*?)]]/", "autoformat__link_s", $line);
|
||||
$line = preg_replace_callback("/\[\[(.*?)]](s?)/", "autoformat__link", $line);
|
||||
return $line;
|
||||
}
|
||||
|
||||
function autoformat__code($matches) {
|
||||
return '<tt>' . syntax_highlight(htmlspecialchars($matches[1])) . '</tt>';
|
||||
$code = str_replace('@@','@',$matches[1]);
|
||||
return '<tt>' . syntax_highlight(htmlspecialchars($code)) . '</tt>';
|
||||
}
|
||||
|
||||
function autoformat__link($matches) {
|
||||
|
||||
@@ -131,7 +131,7 @@ function highlight_script($code) {
|
||||
while(strlen($code)) {
|
||||
if (preg_match("@^<[^>]+>@",$code, $matches)) {
|
||||
$ret .= $matches[0]; // plain tag
|
||||
} else if (preg_match("@^(if|then|else|for( each)?|in(?= )|do|div|mod|and|or|xor|not|rgb|rgba|from|to)\b(?!:)@",$code, $matches)) {
|
||||
} else if (preg_match("@^(if|then|else|for( each)?|in(?= )|do|div|mod|and|or|xor|not|rgb|rgba|from|to|min|max)\b(?!:)@",$code, $matches)) {
|
||||
$ret .= "<span class='hl-kw'>" . $matches[0] . "</span>";
|
||||
} else if (preg_match("@^(include file:)(.*)@",$code, $matches)) {
|
||||
$ret .= "<span class='hl-key'>" . $matches[1] . "</span>" . $matches[2];
|
||||
@@ -150,7 +150,7 @@ function highlight_script($code) {
|
||||
if ($matches[2] == '{') $string .= 's';
|
||||
} else if (preg_match("@^\\#.*@",$code, $matches)) {
|
||||
$ret .= "<span class='hl-comment'>" . $matches[0] . "</span>";
|
||||
} else if (preg_match("@^([-+*/=!.]|<|>)+|^:=@",$code, $matches)) {
|
||||
} else if (preg_match("@^([-+*/=!.\@]|<|>)+|^:=@",$code, $matches)) {
|
||||
$ret .= "<span class='hl-op'>" . $matches[0] . "</span>";
|
||||
} else if (preg_match("@^([}]|[\\(\\)\\[\\]{,]+)@",$code, $matches)) {
|
||||
$ret .= "<span class='hl-paren'>" . $matches[0] . "</span>";
|
||||
|
||||
Reference in New Issue
Block a user