1using System.Globalization;
3using System.Text.RegularExpressions;
90 public static IEnumerable<BadExpression>
Parse(
string fileName,
string source)
116 return op.
Parse(
this);
137 Dictionary<BadExpression, BadExpression[]> cases =
new Dictionary<BadExpression, BadExpression[]>();
138 List<BadExpression>? defaultCase =
null;
158 List<BadExpression> caseBody =
new List<BadExpression>();
164 cases[caseValue] = caseBody.ToArray();
179 Dictionary<BadExpression, BadExpression[]> conditionMap =
new Dictionary<BadExpression, BadExpression[]>();
196 List<BadExpression> block =
ParseBlock(out
bool _);
198 conditionMap[condition] = block.ToArray();
245 List<BadExpression> block =
ParseBlock(out
bool _);
272 List<BadExpression> block =
ParseBlock(out
bool _);
305 List<BadExpression> block =
ParseBlock(out
bool _);
329 StringBuilder rootMeta =
new StringBuilder();
330 StringBuilder returnMeta =
new StringBuilder();
331 string returnType =
"any";
332 Dictionary<string, (string, StringBuilder)> meta =
new Dictionary<
string, (
string, StringBuilder)>();
334 StringBuilder GetMeta(
string name,
string type)
336 if (meta.TryGetValue(name, out (
string, StringBuilder) val))
341 val = (type,
new StringBuilder());
353 string name =
Reader.ParseWord().Text;
363 type +=
Reader.ParseWord().Text;
379 StringBuilder m = GetMeta(name, type);
401 type +=
Reader.ParseWord().Text;
446 rootMeta.AppendLine();
455 rootMeta.ToString().Trim(),
456 returnMeta.ToString().Trim(),
462 x.Value.Item2.ToString().Trim()
485 if (prefixExpr !=
null)
529 List<BadExpression> initExpressions =
new List<BadExpression>();
558 initExpressions.ToArray(),
567 Dictionary<BadWordToken, BadExpression> table =
new Dictionary<BadWordToken, BadExpression>();
720 bool isConstant =
false;
721 bool isStatic =
false;
809 if (
Reader.IsStringQuote() ||
Reader.IsStringQuote(0,
true))
835 if (
Reader.IsNumberStart())
840 decimal.Parse(token.
Text, NumberFormatInfo.InvariantInfo),
847 if (valueParser !=
null)
886 name =
Reader.ParseWord();
992 new List<BadFunctionParameter>
1006 List<BadExpression> typeArgs =
new List<BadExpression>();
1063 bool isNullChecked =
Reader.
Is(
"?[");
1075 bool isReverse =
false;
1083 List<BadExpression> indices =
new List<BadExpression>();
1116 left.Position.Combine(end),
1125 left.Position.Combine(end),
1137 List<BadExpression> args =
new List<BadExpression>();
1188 left = op.
Parse(left,
this);
1205 $
"Expected string start character but got '{(Reader.IsEof() ? "EOF
" : Reader.GetCurrentChar())}'",
1212 StringBuilder sb =
new StringBuilder();
1213 List<BadExpression> args =
new List<BadExpression>();
1215 while (!
Reader.IsStringQuote())
1220 "String not terminated",
1235 sb.Append($
"{{{args.Count}}}");
1273 if (args.Count == 0)
1275 var str = sb.ToString()
1277 .Replace(
"}}",
"}");
1308 $
"Expected string start character but got '{(Reader.IsEof() ? "EOF
" : Reader.GetCurrentChar())}'",
1315 bool isEscaped =
false;
1316 StringBuilder sb =
new StringBuilder();
1317 List<BadExpression> args =
new List<BadExpression>();
1319 while (!
Reader.IsStringQuote())
1324 "String not terminated",
1339 sb.Append($
"{{{args.Count}}}");
1379 sb.Append(Regex.Unescape($
"\\{Reader.GetCurrentChar()}"));
1391 if (args.Count == 0)
1393 var str = sb.ToString()
1395 .Replace(
"}}",
"}");
1422 List<BadExpression> block =
ParseBlock(out
bool _);
1440 List<BadExpression> block =
new List<BadExpression>();
1444 isSingleLine =
false;
1468 isSingleLine =
true;
1499 List<BadExpression> block =
new List<BadExpression>();
1505 isSingleLine =
true;
1509 isSingleLine =
false;
1534 "Expected Expression Body",
1556 "Expected Constant Variable Definition",
1566 "Expected Constant Variable Definition",
1574 "Expected Constant Variable Definition",
1607 "Expected Constant Variable Definition",
1617 "Expected Constant Variable Definition",
1625 "Expected Constant Variable Definition",
1635 List<BadExpression> block =
ParseBlock(out
bool isSingleLine);
1656 List<BadExpression> block =
ParseBlock(out
bool isSingleLine);
1673 List<BadExpression> errorBlock =
ParseBlock(out isSingleLine);
1688 List<BadExpression> finallyExprs =
ParseBlock(out isSingleLine);
1696 finallyBlock = finallyExprs.ToArray();
1702 errorBlock.ToArray(),
1737 string? name =
null;
1764 name =
Reader.ParseWord().Text;
1771 "Expected Variable Expression",
1809 List<BadWordToken> genericParameters =
new List<BadWordToken>();
1817 genericParameters.Add(
Reader.ParseWord());
1830 return genericParameters;
1847 List<BadExpression> interfaces =
new List<BadExpression>();
1871 List<BadInterfaceConstraint> constraints =
new List<BadInterfaceConstraint>();
1878 constraints.Add(expr);
1888 constraints.ToArray(),
1889 interfaces.ToArray(),
1892 genericParameters.ToArray()
1911 List<BadFunctionParameter>? primaryConstructor =
null;
1912 List<BadExpression>? baseInvocationParameters =
null;
1915 List<BadExpression> members =
new List<BadExpression>();
1916 List<BadExpression> staticMembers =
new List<BadExpression>();
1925 List<BadExpression> baseClasses =
new List<BadExpression>();
1935 if (baseClasses.Count == 0)
1939 baseInvocationParameters = baseInvoc.Arguments.ToList();
1940 baseInvocationParametersPosition = baseInvoc.Position;
1941 baseExpr = baseInvoc.Left;
1947 "Base Class Invocation must be the first base class",
1952 baseClasses.Add(baseExpr);
1970 List<BadExpression> attributeExpressions =
new List<BadExpression>();
1976 bool isAttribute =
false;
1985 attributeExpressions.Add(expr);
1995 staticMembers.Add(expr);
1999 if (primaryConstructor !=
null)
2002 "Primary Constructor already defined",
2014 if (attributeExpressions.Count > 0)
2017 attributeExpressions.Clear();
2023 "Class Member cannot have the same name as the class",
2038 if (attributeExpressions.Count != 0)
2041 "Attributes without target",
2054 if (primaryConstructor !=
null)
2056 List<BadExpression> block =
new List<BadExpression>();
2061 if (baseInvocationParameters !=
null)
2066 baseInvocationParameters,
2067 baseInvocationParametersPosition!
2069 block.Add(baseInvocation);
2082 primaryConstructorPosition!
2096 staticMembers.ToArray(),
2097 baseClasses.ToArray(),
2100 genericParameters.ToArray()
2133 if (
Reader.IsWordStart())
2135 name =
Reader.ParseWord().Text;
2136 typeExpr = nameExpr;
2143 "Expected Variable Expression",
2152 bool isOptional =
false;
2153 bool isNullChecked =
false;
2154 bool isRestArgs =
false;
2172 isNullChecked =
true;
2179 isNullChecked =
true;
2205 List<BadFunctionParameter> parameters =
new List<BadFunctionParameter>();
2213 bool hadOptional =
false;
2214 bool hadRest =
false;
2221 "Rest parameter must be last parameter",
2237 if (hadOptional && param is { IsOptional:
false, IsRestArgs:
false })
2240 "Non-Optional parameters must be in front of optional parameters",
2247 if (parameters.Any(p => p.Name == param.
Name))
2250 "Duplicate parameter name",
2259 parameters.Add(param);
2282 string? functionName,
2287 List<BadFunctionParameter> parameters)
2298 if (functionName ==
null)
2345 string? functionName =
null;
2371 functionReturn = functionNameExpr;
2372 functionName =
Reader.ParseWord().Text;
2379 "Expected Variable Expression",
2384 functionName = expr.Name;
2390 return ParseFunction(start, functionName, functionReturn, isConstant, isStatic, compileLevel, parameters);
2410 return expr is not (
2424 IsSingleLine:
false,
2432 public IEnumerable<BadExpression>
Parse()
Describes a specific position inside a source file.
int Index
The Start Index of the Position.
BadSourcePosition Combine(BadSourcePosition other)
Combines two Source Positions.
Contains Static Data for the BadScript Language.
const char ESCAPE_CHARACTER
const string INTERFACE_KEY
const string FOR_EACH_KEY
const string SET_ACCESSOR_KEY
const string MULTI_LINE_STRING_KEY
const string FORMAT_STRING_KEY
const string FUNCTION_KEY
const string CONSTRUCTOR_NAME
const string COMPILED_FAST_DEFINITION_KEY
const string VARIABLE_DEFINITION_KEY
const char STATEMENT_END_KEY
const string GET_ACCESSOR_KEY
const string COMPILED_DEFINITION_KEY
const string CONTINUE_KEY
const string MULTI_LINE_FORMAT_STRING_KEY
const string STATIC_DEFINITION_KEY
const string CONSTANT_DEFINITION_KEY
Gets Raised when a Parser Error occurs.
The Parser of the Language. It turns Source Code into an Expression Tree.
BadStringExpression ParseFormatString()
Parses a Format Expression. Moves the reader to the next token.
BadSourceParser(BadSourceReader sourceReader, BadOperatorTable operators)
Constructor of the Parser.
BadNewExpression ParseNew()
Parses a New Expression. Moves the reader to the next token.
static BadSourceParser Create(string fileName, string source, int start, int end)
Creates a BadSourceParser Instance based on the source and filename provided.
BadExpression ParseLock()
Parses a Lock Expression. Moves the Reader to the Next Token.
IEnumerable< BadExpression > Parse()
Parses the File from start to end.
BadFunctionExpression ParseFunction(int start, string? functionName, BadExpression? functionReturn, bool isConstant, bool isStatic, BadFunctionCompileLevel compileLevel, List< BadFunctionParameter > parameters)
Parses a Function Definition. Moves the reader to the next token.
static bool RequireSemicolon(BadExpression expr)
Returns true if the given expression requires a semicolon.
static BadSourceParser Create(string fileName, string source)
Creates a BadSourceParser Instance based on the source and filename provided.
BadSourceReader Reader
The Source Reader.
List< BadExpression > ParseBlock(out bool isSingleLine)
Parses a Block Expression. Moves the reader to the next token.
BadClassPrototypeExpression ParseClass()
Parses a Class Structure. Moves the reader to the next token.
BadExpression ParseSwitch()
List< BadExpression > ParseGenericArguments()
BadExpression ParseUsingStatement(int start)
Parses a Using Statement Expression. Moves the reader to the next token.
BadExpression ParseValue(int precedence)
Parses a Value Expression or a Prefix Function with precedence greater than the provided precedence....
List< BadExpression > ParseFunctionBlock(int start, out bool isSingleLine)
Parses a Block. Moves the reader to the next token.
BadFunctionExpression ParseFunction(bool isConstant, bool isStatic, BadFunctionCompileLevel compileLevel)
Parses a function definition. Moves the reader to the next token.
BadExpression ParseExpression(BadExpression? left=null, int precedence=int.MaxValue)
Parses an Expression with a precedence greater than the given precedence. Moves the reader to the nex...
BadExpression? ParsePrefix(int precedence)
Parses a Prefix Expression that has precedence greater than the provided precedence....
BadInterfaceConstraint ParseInterfaceConstraint()
Parses an Interface Constraint. Moves the reader to the next token.
BadExpression ParseFor()
Parses a For Loop Expression. Moves the Reader to the Next Token.
BadInterfacePrototypeExpression ParseInterface()
Parses an Interface prototype. Moves the reader to the next token.
BadExpression ParseTry()
Parses a Try Catch Block. Moves the reader to the next token.
readonly BadOperatorTable m_Operators
The Operator Table that is used to parse the Source Code.
BadFunctionParameter ParseParameter()
Parses a Function Parameter. Moves the reader to the next token.
BadExpression ParseUsing()
Parses a Using Block or Statement. Moves the reader to the next token.
List< BadWordToken > ParseGenericParameters()
BadMetaData? m_MetaData
The Meta Data of the current expression.
BadStringExpression ParseMultiLineFormatString()
Parses a Multiline Format string expression. Moves the reader to the next token.
List< BadFunctionParameter > ParseParameters(int start)
Parses a Function Parameter List. Moves the reader to the next token.
BadWhileExpression ParseWhile()
Parses a While Loop. Moves the reader to the next token.
void ParseMeta()
Parses the MetaData of the current expression.
BadExpression ParseForEach()
Parses a For Each Expression. Moves the Reader to the Next Token.
static IEnumerable< BadExpression > Parse(string fileName, string source)
Parses a BadExpression from the Source Reader.
BadExpression ParseIf()
Parses an If Expression. Moves the Reader to the Next Token.
Implements the Array Access to set or get properties from an object. LEFT[RIGHT].
Implements the Reverse Array Access to set or get properties from an object. LEFT[^RIGHT].
Implements the Member Access to set or get properties from an object. LEFT.RIGHT.
Base Implementation for all Expressions used inside the Script.
BadSourcePosition Position
The source Position of the Expression.
void SetAttributes(IEnumerable< BadExpression > attributes)
Implements the Assign Expression LEFT = RIGHT.
Implements the If Statement Expression.
Implements the Switch Statement Expression.
Implements the Try Catch Statement Expression.
Implements the Using Block Expression.
Implements the Using Statement Expression.
Implements the Lock Expression.
Implements the For Each Expression.
Implements the For Loop Expression.
Implements the While Expression.
Implements the Array Expression.
Implements the Boolean Expression.
Implements the Null Expression.
Implements the Number Expression.
Implements the String Expression.
Implements the Table Expression.
Implements the Break Expression that is used to prematurely exit a loop.
Implements the Break Expression that is used to skip a loop iteraion.
Implements the Return expression that is used to exit the current function with an Optional Return Va...
Implements the Throw Expression that is used to raise errors inside the Script.
Implements the Function Expression.
BadWordToken? Name
The (optional) Function Name.
Implements the Invocation Expression.
Exports the Default Value of the current execution.
Exports a Named Value from the current execution.
Implements the Class Prototype Expression.
Implements a Function Constraint for an Interface The Constraints specifies how a specific function s...
Implements an Interface Prototype Expression.
Implements the New Expression.
Implements the Variable Definition Expression.
Implements the Variable Expression.
Base class for all binary operators.
BadExpression Parse(BadExpression left, BadSourceParser parser)
Parses the operator.
Implements the Operator Table used by the Parser.
IEnumerable< string > BinarySymbols
Enumeration of all Binary Operator Symbols.
BadValueParser? GetValueParser(BadSourceParser parser)
Returns a Value Parser that is able to parse the given Token.
IEnumerable< string > UnaryPrefixSymbols
Enumeration of all Unary Prefix Operator Symbols.
BadBinaryOperator? FindBinaryOperator(string symbol, int precedence)
Finds a Binary Operator by its Symbol.
BadUnaryPrefixOperator? FindUnaryPrefixOperator(string symbol, int precedence)
Finds a Unary Prefix Operator by its Symbol.
static BadOperatorTable Instance
The Operator Table Instance.
Base class for all Unary Prefix Operators.
BadExpression Parse(BadSourceParser parser)
Parses the Operator and returns the resulting Expression.
Base class for all Value Parsers.
BadExpression ParseValue(BadSourceParser parser)
Parses the Current Token.
Gets Raised if the Reader encounters an Error.
Implements the Source Code Reader.
void MoveNext()
Moves the Reader to the next character in the source code.
void SetPosition(int index)
Sets the Current Index of the Reader.
char GetCurrentChar(int offset=0)
Returns the Current Character.
BadSourcePosition MakeSourcePosition(int length)
Creates a source position with the specified length and the current index of the reader.
int CurrentIndex
The Current Index of the Reader.
bool IsEof(int offset=0)
Returns true if the reader is at the end of the source code.
BadSourcePosition Eat(char c)
Asserts that the current character matches the specified character.
bool Is(char c, int offset=0)
Returns true if the current character matches the specified character.
Implements a Symbol Token.
string Text
The Text Representation of the Token.
BadSourcePosition SourcePosition
The Source Position of the Token.
Implements a Token that represents a Boolean.
Implements a Token that represents a Number.
Implements a Token that represents a String.
string Value
The Value of the Token.
Gets thrown by the runtime.
Provides function parameter info.
bool IsOptional
Indicates if this parameter is optional.
BadExpression? TypeExpr
The Expression that returns the type of the parameter if evaluated.
bool IsRestArgs
Indicates if this parameter is the rest parameter of the function.
string Name
The Name of the Parameter.
Implements an Interface Constraint.
Gets inherited by all Expressions that have a Name(e.g. Variable Definitions, Function Definitions,...
string? GetName()
Returns the Name of the Expression.
Contains Shared Data Structures and Functionality.
Contains the Access Expressions for the BadScript2 Language.
Contains the Binary Expressions for the BadScript2 Language.
Contains the Locking Expressions for the BadScript2 Language.
Contains the Loop Expressions for the BadScript2 Language.
Contains the Block Expressions for the BadScript2 Language.
Contains the Constant Expressions for the BadScript2 Language.
Contains the Controlflow Expressions for the BadScript2 Language.
Contains the Function Expressions for the BadScript2 Language.
BadFunctionCompileLevel
The BadFunctionCompileLevel enum defines the different levels of compilation for a function.
Contains the Type Expressions for the BadScript2 Language.
Contains the Variable Expressions for the BadScript2 Language.
Contains the Expressions for the BadScript2 Language.
Contains the Operators for the BadScript2 Language.
Contains the Parser for the BadScript2 Language.
Contains the Primitive Tokens for the BadScript2 Language.
Contains the Reader Tokens for the BadScript2 Language.
Contains the Source Reader for the BadScript2 Language.
Contains the Error Objects for the BadScript2 Language.
Contains Runtime Function Objects.
Contains Runtime Interface Objects.