本文整理汇总了C#中Neo.IronLua.LuaLexer类的典型用法代码示例。如果您正苦于以下问题:C# LuaLexer类的具体用法?C# LuaLexer怎么用?C# LuaLexer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
LuaLexer类属于Neo.IronLua命名空间,在下文中一共展示了LuaLexer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TokenTest
} // func T
private bool TokenTest(string sToken, params KeyValuePair<LuaToken, string>[] token)
{
using (LuaLexer l = new LuaLexer("test.lua", new StringReader(sToken)))
{
l.Next();
for (int i = 0; i < token.Length; i++)
{
Debug.Write(String.Format("Test: {0} = {1} ==>", l.Current.Typ, token[i].Key));
if (l.Current.Typ != token[i].Key)
{
Debug.WriteLine("tokens FAILED");
return false;
}
else if (l.Current.Value != token[i].Value)
{
Debug.WriteLine("values '{0}' != '{1}' FAILED", l.Current.Value, token[i].Value);
return false;
}
Debug.WriteLine("OK");
l.Next();
}
if (l.Current.Typ != LuaToken.Eof)
return false;
return true;
}
} // func TokenTest
示例2: ParseChunk
/// <summary>Parses the chunk to an function.</summary>
/// <param name="runtime">Binder</param>
/// <param name="options">Compile options for the script.</param>
/// <param name="lHasEnvironment">Creates the _G parameter.</param>
/// <param name="code">Lexer for the code.</param>
/// <param name="typeDelegate">Type for the delegate. <c>null</c>, for an automatic type</param>
/// <param name="returnType">Defines the return type of the chunk.</param>
/// <param name="args">Arguments of the function.</param>
/// <returns>Expression-Tree for the code.</returns>
public static LambdaExpression ParseChunk(Lua runtime, LuaCompileOptions options, bool lHasEnvironment, LuaLexer code, Type typeDelegate, Type returnType, IEnumerable<KeyValuePair<string, Type>> args)
{
List<ParameterExpression> parameters = new List<ParameterExpression>();
if (returnType == null)
returnType = typeof(LuaResult);
var globalScope = new GlobalScope(runtime, options, returnType, returnType == typeof(LuaResult) ? Expression.Property(null, Lua.ResultEmptyPropertyInfo) : null);
// Registers the global LuaTable
if (lHasEnvironment)
parameters.Add(globalScope.RegisterParameter(typeof(LuaTable), csEnv));
if (args != null)
{
foreach (var c in args)
parameters.Add(globalScope.RegisterParameter(c.Value, c.Key)); // Add alle arguments
}
// Get the first token
if (code.Current == null)
code.Next();
// Get the name for the chunk and clean it from all unwanted chars
string sChunkName = CreateNameFromFile(code.Current.Start.FileName);
if ((globalScope.EmitDebug & LuaDebugLevel.RegisterMethods) == LuaDebugLevel.RegisterMethods)
sChunkName = Lua.RegisterUniqueName(sChunkName);
// Create the block
ParseBlock(globalScope, code);
if (code.Current.Typ != LuaToken.Eof)
throw ParseError(code.Current, Properties.Resources.rsParseEof);
// Create the function
return typeDelegate == null ?
Expression.Lambda(globalScope.ExpressionBlock, sChunkName, parameters) :
Expression.Lambda(typeDelegate, globalScope.ExpressionBlock, sChunkName, parameters);
}
示例3: ParseTableConstructor
private static Expression ParseTableConstructor(Scope scope, LuaLexer code)
{
// table ::= '{' [field] { fieldsep field } [fieldsep] '}'
// fieldsep ::= ',' | ';'
FetchToken(LuaToken.BracketCurlyOpen, code);
if (code.Current.Typ != LuaToken.BracketCurlyClose)
{
int iIndex = 1;
Scope scopeTable = new Scope(scope);
// Create the variable for the table
ParameterExpression tableVar = scopeTable.RegisterVariable(typeof(LuaTable), "#table");
scopeTable.AddExpression(Expression.Assign(tableVar, CreateEmptyTableExpression()));
// fiest field
ParseTableField(tableVar, scopeTable, code, ref iIndex);
// collect more table fields
while (code.Current.Typ == LuaToken.Comma || code.Current.Typ == LuaToken.Semicolon)
{
code.Next();
// Optional last separator
if (code.Current.Typ == LuaToken.BracketCurlyClose)
break;
// Parse the field
ParseTableField(tableVar, scopeTable, code, ref iIndex);
}
scopeTable.AddExpression(tableVar);
scopeTable.ExpressionBlockType = typeof(LuaTable);
// Closing bracket
FetchToken(LuaToken.BracketCurlyClose, code);
return scopeTable.ExpressionBlock;
}
else
{
FetchToken(LuaToken.BracketCurlyClose, code);
return CreateEmptyTableExpression();
}
}
示例4: ParseFirstType
private static LuaType ParseFirstType(Scope scope, LuaLexer code)
{
string sType = FetchToken(LuaToken.Identifier, code).Value;
LuaType luaType = LuaType.GetCachedType(sType);
if (luaType == null)
{
ConstantExpression expr = scope.LookupExpression(sType, false) as ConstantExpression;
if (expr != null && expr.Type == typeof(LuaType))
return (LuaType)expr.Value;
else
return LuaType.GetType(LuaType.Clr.GetIndex(sType, false, null));
}
else
return luaType;
}
示例5: ParseExpressionStatement
private static void ParseExpressionStatement(Scope scope, LuaLexer code, bool lLocal)
{
List<ParameterExpression> registerLocals = null;
List<PrefixMemberInfo> prefixes = new List<PrefixMemberInfo>();
// parse the assgiee list (var0, var1, var2, ...)
while (true)
{
if (lLocal) // parse local variables
{
Token tVar;
Type typeVar;
ParseIdentifierAndType(scope, code, out tVar, out typeVar);
ParameterExpression exprVar = scope.LookupExpression(tVar.Value, true) as ParameterExpression;
if (exprVar == null)
{
exprVar = Expression.Variable(typeVar, tVar.Value);
if (registerLocals == null)
registerLocals = new List<ParameterExpression>();
registerLocals.Add(exprVar);
}
else if (exprVar.Type != typeVar)
throw ParseError(tVar, Properties.Resources.rsParseTypeRedef);
prefixes.Add(new PrefixMemberInfo(tVar, exprVar, null, null, null));
}
else // parse a assignee
{
// parse as a prefix
prefixes.Add(ParsePrefix(scope, code));
}
// is there another prefix
if (code.Current.Typ == LuaToken.Comma)
code.Next();
else
break;
}
// Optional assign
if (code.Current.Typ == LuaToken.Assign)
{
code.Next();
// parse all expressions
IEnumerator<Expression> expr = ParseExpressionList(scope, code).GetEnumerator();
expr.MoveNext();
if (prefixes.Count == 1) // one expression, one variable?
{
scope.AddExpression(
prefixes[0].GenerateSet(scope, expr.Current != null ? expr.Current : Expression.Constant(null, typeof(object)))
);
}
else if (expr.Current == null) // No expression, assign null
{
for (int i = 0; i < prefixes.Count; i++)
scope.AddExpression(prefixes[i].GenerateSet(scope, Expression.Constant(null, typeof(object))));
}
else // assign on an unknown number of expressions
{
#region -- unknown number --
List<ParameterExpression> assignTempVars = new List<ParameterExpression>();
List<Expression> assignExprs = new List<Expression>();
int iExpressionVarOffset;
// Safe the prefixes in variables
for (int k = 0; k < prefixes.Count; k++)
{
var p = prefixes[k];
if (p.Member != null || prefixes[k].Indices != null)
{
p.Instance = ParseExpressionStatementExchangeToTempVar(assignTempVars, assignExprs, p.Instance);
if (p.Indices != null)
{
for (int l = 0; l < p.Indices.Length; l++)
p.Indices[l] = ParseExpressionStatementExchangeToTempVar(assignTempVars, assignExprs, p.Indices[l]);
}
}
}
// collect the results of the expressions
iExpressionVarOffset = assignTempVars.Count;
do
{
ParseExpressionStatementExchangeToTempVar(assignTempVars, assignExprs, expr.Current);
} while (expr.MoveNext());
// Assign the Result to the prefixes
int i = 0;
int j = 0;
ParameterExpression lastVariable = null;
while (i < prefixes.Count)
{
if (i < assignTempVars.Count - iExpressionVarOffset) // are the variables
{
if (i == assignTempVars.Count - iExpressionVarOffset - 1 && assignTempVars[i + iExpressionVarOffset].Type == typeof(LuaResult)) // check if the last expression is a LuaResult
{
//.........这里部分代码省略.........
示例6: ParseExpressionPower
private static Expression ParseExpressionPower(Scope scope, LuaLexer code, InvokeResult result, ref bool lWrap)
{
// expPow ::= cast [ ^ expPow ]
Expression expr = ParseExpressionCast(scope, code, result, ref lWrap);
if (code.Current.Typ == LuaToken.Caret)
{
code.Next();
lWrap |= true;
return BinaryOperationExpression(scope.Runtime, code.Current, ExpressionType.Power, expr, ParseExpressionPower(scope, code, InvokeResult.Object, ref lWrap));
}
else
return expr;
}
示例7: ParseExpressionOr
private static Expression ParseExpressionOr(Scope scope, LuaLexer code, InvokeResult result, ref bool lWrap)
{
// exprOr ::= exprAnd { or exprAnd }
var expr = ParseExpressionAnd(scope, code, result, ref lWrap);
while (code.Current.Typ == LuaToken.KwOr)
{
code.Next();
expr = BinaryOperationExpression(scope.Runtime, code.Current, ExpressionType.OrElse, expr, ParseExpressionAnd(scope, code, InvokeResult.Object, ref lWrap));
lWrap |= true;
}
return expr;
}
示例8: ParseExpressionList
private static IEnumerable<Expression> ParseExpressionList(Scope scope, LuaLexer code)
{
while (true)
{
yield return ParseExpression(scope, code, InvokeResult.LuaResult, scope.EmitExpressionDebug);
// Noch eine Expression
if (code.Current.Typ == LuaToken.Comma)
code.Next();
else
break;
}
}
示例9: ParseBreak
private static void ParseBreak(Scope scope, LuaLexer code)
{
FetchToken(LuaToken.KwBreak, code);
// Erzeuge die Expression
scope.AddExpression(Expression.Goto(scope.LookupLabel(null, csBreakLabel)));
// Optionales Semicolon
FetchToken(LuaToken.Semicolon, code, true);
}
示例10: ParseBlock
private static void ParseBlock(Scope scope, LuaLexer code)
{
// Lese die Statement
int iLastDebugInfo = -1;
bool lLoop = true;
while (lLoop)
{
bool lDebugInfoEmitted = false;
if ((scope.EmitDebug & LuaDebugLevel.Line) != 0) // debug info for line
{
if (code.Current.Start.Line != iLastDebugInfo)
{
iLastDebugInfo = code.Current.Start.Line;
scope.AddExpression(GetDebugInfo(code.Current, code.Current));
lDebugInfoEmitted = true;
}
}
switch (code.Current.Typ)
{
case LuaToken.Eof: // End of file
lLoop = false;
break;
case LuaToken.KwReturn: // The return-statement is only allowed on the end of a scope
ParseReturn(scope, code);
break;
case LuaToken.KwBreak: // The break-statement is only allowed on the end of a scope
ParseBreak(scope, code);
lLoop = false;
break;
case LuaToken.Semicolon: // End of statement => ignore
code.Next();
break;
default:
if (!lDebugInfoEmitted && (scope.EmitDebug & LuaDebugLevel.Expression) != 0) // Start every statement with a debug point
scope.AddExpression(GetDebugInfo(code.Current, code.Current));
if (!ParseStatement(scope, code)) // Parse normal statements
lLoop = false;
break;
}
}
if (scope.EmitDebug != LuaDebugLevel.None)
scope.AddExpression(Expression.ClearDebugInfo(code.Current.Start.Document)); // Clear debug info
}
示例11: ParseArgumentList
private static ArgumentsList ParseArgumentList(Scope scope, LuaLexer code)
{
FetchToken(LuaToken.BracketOpen, code);
// exprArgumentList := '(' [ exprArg { , exprArg } ] ')'
var argumentsList = new ArgumentsList();
while (code.Current.Typ != LuaToken.BracketClose)
{
Token tName = null;
if (code.LookAhead.Typ == LuaToken.Assign) // named argument
{
tName = FetchToken(LuaToken.Identifier, code);
code.Next(); // equal
}
// parse the expression
var tFirst = code.Current;
var expr = ParseExpression(scope, code, InvokeResult.LuaResult, scope.EmitExpressionDebug);
if (tName == null)
argumentsList.AddPositionalArgument(tFirst, expr);
else
argumentsList.AddNamedArgument(tName, expr);
// optinal comma
FetchToken(LuaToken.Comma, code, true);
}
code.Next();
return argumentsList;
}
示例12: IsExpressionStart
private static bool IsExpressionStart(LuaLexer code)
{
return code.Current.Typ == LuaToken.BracketOpen ||
code.Current.Typ == LuaToken.Identifier ||
code.Current.Typ == LuaToken.DotDotDot ||
code.Current.Typ == LuaToken.String ||
code.Current.Typ == LuaToken.Number ||
code.Current.Typ == LuaToken.KwTrue ||
code.Current.Typ == LuaToken.KwFalse ||
code.Current.Typ == LuaToken.KwNil ||
code.Current.Typ == LuaToken.BracketCurlyOpen ||
code.Current.Typ == LuaToken.Minus ||
code.Current.Typ == LuaToken.Dilde ||
code.Current.Typ == LuaToken.Cross ||
code.Current.Typ == LuaToken.KwNot ||
code.Current.Typ == LuaToken.KwFunction ||
code.Current.Typ == LuaToken.KwCast;
}
示例13: ParseWhileLoop
private static void ParseWhileLoop(Scope scope, LuaLexer code)
{
// while expr do block end;
LoopScope loopScope = new LoopScope(scope);
// get the expression
FetchToken(LuaToken.KwWhile, code);
loopScope.AddExpression(Expression.Label(loopScope.ContinueLabel));
loopScope.AddExpression(
Expression.IfThenElse(
ConvertExpression(scope.Runtime, code.Current, ParseExpression(scope, code, InvokeResult.Object, scope.EmitExpressionDebug), typeof(bool)),
Expression.Empty(),
Expression.Goto(loopScope.BreakLabel)
)
);
// append the block
FetchToken(LuaToken.KwDo, code);
ParseBlock(loopScope, code);
FetchToken(LuaToken.KwEnd, code);
// goto continue
loopScope.AddExpression(Expression.Goto(loopScope.ContinueLabel));
loopScope.AddExpression(Expression.Label(loopScope.BreakLabel));
scope.AddExpression(loopScope.ExpressionBlock);
}
示例14: ParseType
private static LuaType ParseType(Scope scope, LuaLexer code, bool lNeedType)
{
// is the first token an alias
LuaType currentType = ParseFirstType(scope, code);
while (code.Current.Typ == LuaToken.Dot ||
code.Current.Typ == LuaToken.Plus ||
code.Current.Typ == LuaToken.BracketSquareOpen)
{
if (code.Current.Typ == LuaToken.BracketSquareOpen)
{
List<LuaType> genericeTypes = new List<LuaType>();
code.Next();
if (code.Current.Typ != LuaToken.BracketSquareClose)
{
genericeTypes.Add(ParseType(scope, code, lNeedType));
while (code.Current.Typ == LuaToken.Comma)
{
code.Next();
genericeTypes.Add(ParseType(scope, code, lNeedType));
}
}
FetchToken(LuaToken.BracketSquareClose, code);
if (genericeTypes.Count == 0) // create a array at the end
{
if (currentType.Type == null)
throw ParseError(code.Current, String.Format(Properties.Resources.rsParseUnknownType, currentType.FullName));
currentType = LuaType.GetType(currentType.GetIndex("[]", false, () => currentType.Type.MakeArrayType()));
}
else // build a generic type
{
var typeGeneric = LuaType.GetType(currentType.FullName + "`" + genericeTypes.Count.ToString()).Type;
if (typeGeneric == null)
throw ParseError(code.Current, String.Format(Properties.Resources.rsParseUnknownType, currentType.FullName));
currentType = LuaType.GetType(currentType.GetGenericItem(typeGeneric, genericeTypes.ToArray()));
}
}
else
{
code.Next();
currentType = LuaType.GetType(currentType.GetIndex(FetchToken(LuaToken.Identifier, code).Value, false, null));
}
}
if (lNeedType && currentType.Type == null)
throw ParseError(code.Current, String.Format(Properties.Resources.rsParseUnknownType, currentType.FullName));
return currentType;
}
示例15: ParseTableField
private static void ParseTableField(ParameterExpression tableVar, Scope scope, LuaLexer code, ref int iIndex)
{
// field ::= '[' exp ']' '=' exp | Name '=' exp | exp
if (code.Current.Typ == LuaToken.BracketSquareOpen)
{
// Parse the index
code.Next();
var index = ParseExpression(scope, code, InvokeResult.Object, scope.EmitExpressionDebug);
FetchToken(LuaToken.BracketSquareClose, code);
FetchToken(LuaToken.Assign, code);
// Expression that results in a value
scope.AddExpression(
IndexSetExpression(scope.Runtime, code.Current, tableVar, new Expression[] { index },
ParseExpression(scope, code, InvokeResult.Object, scope.EmitExpressionDebug)
)
);
}
else if (code.Current.Typ == LuaToken.Identifier && code.LookAhead.Typ == LuaToken.Assign)
{
// Read the identifier
Token tMember = code.Current;
code.Next();
FetchToken(LuaToken.Assign, code);
// Expression
scope.AddExpression(
IndexSetExpression(scope.Runtime, code.Current, tableVar, new Expression[] { Expression.Constant(tMember.Value) },
ParseExpression(scope, code, InvokeResult.Object, scope.EmitExpressionDebug)
)
);
}
else
{
Token tStart = code.Current;
Expression expr = ParseExpression(scope, code, InvokeResult.None, scope.EmitExpressionDebug);
// Last assign, enroll parameter
if (code.Current.Typ == LuaToken.BracketCurlyClose && LuaEmit.IsDynamicType(expr.Type))
{
scope.AddExpression(
Expression.Call(Lua.TableSetObjectsMethod,
tableVar,
Expression.Convert(expr, typeof(object)),
Expression.Constant(iIndex, typeof(int))
)
);
}
else // Normal index set
{
scope.AddExpression(
IndexSetExpression(scope.Runtime, code.Current, tableVar, new Expression[] { Expression.Constant(iIndex++, typeof(object)) }, expr)
);
}
}
}