1using System.Globalization;
3using System.Text.RegularExpressions;
90 public static IEnumerable<BadExpression>
Parse(
string fileName,
string source)
92 return Create(fileName, source)
116 return op.
Parse(
this);
137 Dictionary<BadExpression, BadExpression[]> cases =
new Dictionary<BadExpression, BadExpression[]>();
138 List<BadExpression>? defaultCase =
null;
162 List<BadExpression> caseBody =
new List<BadExpression>();
170 cases[caseValue] = caseBody.ToArray();
190 Dictionary<BadExpression, BadExpression[]> conditionMap =
new Dictionary<BadExpression, BadExpression[]>();
207 List<BadExpression> block =
ParseBlock(out
bool _);
209 conditionMap[condition] = block.ToArray();
256 List<BadExpression> block =
ParseBlock(out
bool _);
282 List<BadExpression> block =
ParseBlock(out
bool _);
314 List<BadExpression> block =
ParseBlock(out
bool _);
337 StringBuilder rootMeta =
new StringBuilder();
338 StringBuilder returnMeta =
new StringBuilder();
339 string returnType =
"any";
340 Dictionary<string, (string, StringBuilder)> meta =
new Dictionary<
string, (
string, StringBuilder)>();
342 StringBuilder GetMeta(
string name,
string type)
344 if (meta.TryGetValue(name, out (
string, StringBuilder) val))
349 val = (type,
new StringBuilder());
362 string name =
Reader.ParseWord()
373 type +=
Reader.ParseWord()
390 StringBuilder m = GetMeta(name, type);
412 type +=
Reader.ParseWord()
458 rootMeta.AppendLine();
468 returnMeta.ToString()
471 meta.ToDictionary(x => x.Key,
473 x.Value.Item2.ToString()
497 if (prefixExpr !=
null)
541 List<BadExpression> initExpressions =
new List<BadExpression>();
578 Dictionary<BadWordToken, BadExpression> table =
new Dictionary<BadWordToken, BadExpression>();
730 bool isConstant =
false;
731 bool isStatic =
false;
818 if (
Reader.IsStringQuote() ||
Reader.IsStringQuote(0,
true))
842 if (
Reader.IsNumberStart())
853 if (valueParser !=
null)
892 name =
Reader.ParseWord();
1009 new List<BadFunctionParameter> { p }
1020 List<BadExpression> typeArgs =
new List<BadExpression>();
1081 bool isNullChecked =
Reader.
Is(
"?[");
1093 bool isReverse =
false;
1101 List<BadExpression> indices =
new List<BadExpression>();
1133 left.Position.Combine(end),
1141 left.Position.Combine(end),
1153 List<BadExpression> args =
new List<BadExpression>();
1203 left = op.
Parse(left,
this);
1220 BadSourceReaderException($
"Expected string start character but got '{(Reader.IsEof() ? "EOF
" : Reader.GetCurrentChar())}'",
1227 StringBuilder sb =
new StringBuilder();
1228 List<BadExpression> args =
new List<BadExpression>();
1230 while (!
Reader.IsStringQuote())
1249 sb.Append($
"{{{args.Count}}}");
1286 if (args.Count == 0)
1288 string str = sb.ToString()
1290 .Replace(
"}}",
"}");
1320 BadSourceReaderException($
"Expected string start character but got '{(Reader.IsEof() ? "EOF
" : Reader.GetCurrentChar())}'",
1327 bool isEscaped =
false;
1328 StringBuilder sb =
new StringBuilder();
1329 List<BadExpression> args =
new List<BadExpression>();
1331 while (!
Reader.IsStringQuote())
1350 sb.Append($
"{{{args.Count}}}");
1389 sb.Append(Regex.Unescape($
"\\{Reader.GetCurrentChar()}"));
1401 if (args.Count == 0)
1403 string str = sb.ToString()
1405 .Replace(
"}}",
"}");
1431 List<BadExpression> block =
ParseBlock(out
bool _);
1448 List<BadExpression> block =
new List<BadExpression>();
1452 isSingleLine =
false;
1476 isSingleLine =
true;
1507 List<BadExpression> block =
new List<BadExpression>();
1513 isSingleLine =
true;
1517 isSingleLine =
false;
1643 List<BadExpression> block =
ParseBlock(out
bool isSingleLine);
1669 List<BadExpression> block =
ParseBlock(out
bool isSingleLine);
1686 List<BadExpression> errorBlock =
ParseBlock(out isSingleLine);
1702 List<BadExpression> finallyExprs =
ParseBlock(out isSingleLine);
1711 finallyBlock = finallyExprs.ToArray();
1716 errorBlock.ToArray(),
1751 string? name =
null;
1779 name =
Reader.ParseWord()
1822 List<BadWordToken> genericParameters =
new List<BadWordToken>();
1832 genericParameters.Add(
Reader.ParseWord());
1848 return genericParameters;
1865 List<BadExpression> interfaces =
new List<BadExpression>();
1889 List<BadInterfaceConstraint> constraints =
new List<BadInterfaceConstraint>();
1896 constraints.Add(expr);
1905 constraints.ToArray(),
1906 interfaces.ToArray(),
1909 genericParameters.ToArray()
1928 List<BadFunctionParameter>? primaryConstructor =
null;
1929 List<BadExpression>? baseInvocationParameters =
null;
1932 List<BadExpression> members =
new List<BadExpression>();
1933 List<BadExpression> staticMembers =
new List<BadExpression>();
1943 List<BadExpression> baseClasses =
new List<BadExpression>();
1954 if (baseClasses.Count == 0)
1958 baseInvocationParameters = baseInvoc.Arguments.ToList();
1959 baseInvocationParametersPosition = baseInvoc.Position;
1960 baseExpr = baseInvoc.Left;
1970 baseClasses.Add(baseExpr);
1988 List<BadExpression> attributeExpressions =
new List<BadExpression>();
1994 bool isAttribute =
false;
2006 attributeExpressions.Add(expr);
2014 staticMembers.Add(expr);
2018 if (primaryConstructor !=
null)
2033 if (attributeExpressions.Count > 0)
2036 attributeExpressions.Clear();
2056 if (attributeExpressions.Count != 0)
2072 if (primaryConstructor !=
null)
2074 List<BadExpression> block =
new List<BadExpression>();
2080 primaryConstructorPosition!,
2088 if (baseInvocationParameters !=
null)
2093 baseInvocationParameters,
2094 baseInvocationParametersPosition!
2096 block.Add(baseInvocation);
2109 primaryConstructorPosition!,
2110 new List<BadExpression>()
2113 primaryConstructorPosition!
2115 primaryConstructorPosition!
2122 primaryConstructorPosition!,
2133 staticMembers.ToArray(),
2134 baseClasses.ToArray(),
2137 genericParameters.ToArray()
2170 if (
Reader.IsWordStart())
2172 name =
Reader.ParseWord()
2174 typeExpr = nameExpr;
2188 bool isOptional =
false;
2189 bool isNullChecked =
false;
2190 bool isRestArgs =
false;
2208 isNullChecked =
true;
2215 isNullChecked =
true;
2241 List<BadFunctionParameter> parameters =
new List<BadFunctionParameter>();
2249 bool hadOptional =
false;
2250 bool hadRest =
false;
2271 if (hadOptional && param is { IsOptional:
false, IsRestArgs:
false })
2273 throw new BadParserException(
"Non-Optional parameters must be in front of optional parameters",
2280 if (parameters.Any(p => p.Name == param.
Name))
2290 parameters.Add(param);
2312 string? functionName,
2317 List<BadFunctionParameter> parameters)
2328 if (functionName ==
null)
2373 string? functionName =
null;
2400 functionReturn = functionNameExpr;
2402 functionName =
Reader.ParseWord()
2414 functionName = expr.Name;
2420 return ParseFunction(start, functionName, functionReturn, isConstant, isStatic, compileLevel, parameters);
2440 return expr is not (
2458 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.