private static List <Token> GetTernaryConfirmation(List <Token> tokens) { int questionIndex = TokenGroups.IndexOfTokenOutsideBrackets(tokens, TokenType.QuestionMark); int colonIndex = TokenGroups.IndexOfTokenOutsideBrackets(tokens, TokenType.Colon); return(tokens.GetRange(questionIndex + 1, colonIndex - questionIndex - 1)); }
private static INumerable BuildCount(List <Token> tokens) { List <Token> laterTokens = tokens.Skip(1).ToList(); if (laterTokens.Count == 0) { throw new SyntaxErrorException("ERROR! Expression 'count' do not contain all necessary information."); } if (TokenGroups.ContainsTokenOutsideBrackets(laterTokens, TokenType.Inside)) { return(BuildCountInside(laterTokens)); } else { IListable ilist = ListableBuilder.Build(laterTokens); if (ilist.IsNull()) { return(null); } else { return(new Count(ilist)); } } }
private static INumerable BuildCountInside(List <Token> tokens) { int index = TokenGroups.IndexOfTokenOutsideBrackets(tokens, TokenType.Inside); if (index == tokens.Count - 1) { throw new SyntaxErrorException("ERROR! Expression 'count inside' do not contain information about referent location."); } if (index == 0) { throw new SyntaxErrorException("ERROR! Expression 'count inside' do not contain information about referent list of elements."); } IListable ilist = ListableBuilder.Build(tokens.Take(index).ToList()); if (ilist.IsNull()) { return(null); } IStringable istr = StringableBuilder.Build(tokens.Skip(index + 1).ToList()); if (istr.IsNull()) { return(null); } return(new CountInside(ilist, istr)); }
private static ComparisonType GetComparingToken(List <Token> tokens) { TokenType ttype = (tokens.Where(x => TokenGroups.IsComparingSign(x.GetTokenType())).First()).GetTokenType(); switch (ttype) { case TokenType.Equals: return(ComparisonType.Equals); case TokenType.NotEquals: return(ComparisonType.NotEquals); case TokenType.Bigger: return(ComparisonType.Bigger); case TokenType.BiggerOrEquals: return(ComparisonType.BiggerOrEquals); case TokenType.Smaller: return(ComparisonType.Smaller); case TokenType.SmallerOrEquals: return(ComparisonType.SmallerOrEquals); } return(ComparisonType.Equals); }
private static bool ContainsLogicTokens(List <Token> tokens) { int level = 0; int index = 0; foreach (Token tok in tokens) { if (tok.GetTokenType().Equals(TokenType.BracketOn)) { level++; } else if (tok.GetTokenType().Equals(TokenType.BracketOff)) { level--; } else if (TokenGroups.IsLogicSign(tok.GetTokenType())) { if (level == 0) { return(true); } } index++; } return(false); }
public static bool IsPossibleTernary(List <Token> tokens) { int questionIndex = TokenGroups.IndexOfTokenOutsideBrackets(tokens, TokenType.QuestionMark); int colonIndex = TokenGroups.IndexOfTokenOutsideBrackets(tokens, TokenType.Colon); // there is no question mark / colon if (questionIndex == -1 || colonIndex == -1) { return(false); } // question mark is after colon if (questionIndex > colonIndex) { return(false); } // colon comes right after question mark if (colonIndex == questionIndex + 1) { return(false); } // expression starts with question mark or ends with colon if (questionIndex == 0 || colonIndex == tokens.Count - 1) { return(false); } return(true); }
/// <summary> /// Gets a list of the principal's token groups /// </summary> /// <returns>A list of security identifiers representing the groups present in the principal's security token</returns> public IEnumerable <SecurityIdentifier> GetTokenGroups() { uint sizeRequired = 0; if (!NativeMethods.AuthzGetInformationFromContext(this.authzContext, AuthzContextInformationClass.AuthzContextInfoGroupsSids, sizeRequired, out sizeRequired, IntPtr.Zero)) { Win32Exception e = new Win32Exception(Marshal.GetLastWin32Error()); if (e.NativeErrorCode != NativeMethods.InsufficientBuffer) { throw new AuthorizationContextException("AuthzGetInformationFromContext failed", e); } } SafeAllocHGlobalHandle structure = new SafeAllocHGlobalHandle(sizeRequired); IntPtr pstructure = structure.DangerousGetHandle(); if (!NativeMethods.AuthzGetInformationFromContext(this.authzContext, AuthzContextInformationClass.AuthzContextInfoGroupsSids, sizeRequired, out sizeRequired, pstructure)) { throw new AuthorizationContextException("AuthzGetInformationFromContext failed", new Win32Exception(Marshal.GetLastWin32Error())); } TokenGroups groups = Marshal.PtrToStructure <TokenGroups>(pstructure); IntPtr current = IntPtr.Add(pstructure, Marshal.OffsetOf <TokenGroups>(nameof(groups.Groups)).ToInt32()); for (int i = 0; i < groups.GroupCount; i++) { SidAndAttributes sidAndAttributes = (SidAndAttributes)Marshal.PtrToStructure(current, typeof(SidAndAttributes)); yield return(new SecurityIdentifier(sidAndAttributes.Sid)); current = IntPtr.Add(current, Marshal.SizeOf(typeof(SidAndAttributes))); } }
public static TokenHandle Create( TokenAccess access, string name, ObjectFlags objectFlags, DirectoryHandle rootDirectory, TokenType tokenType, Luid authenticationId, long expirationTime, Sid user, Sid[] groups, PrivilegeSet privileges, Sid owner, Sid primaryGroup, Acl defaultDacl, TokenSource source ) { NtStatus status; TokenUser tokenUser = new TokenUser(user); TokenGroups tokenGroups = new TokenGroups(groups); TokenPrivileges tokenPrivileges = new TokenPrivileges(privileges); TokenOwner tokenOwner = new TokenOwner(owner); TokenPrimaryGroup tokenPrimaryGroup = new TokenPrimaryGroup(primaryGroup); TokenDefaultDacl tokenDefaultDacl = new TokenDefaultDacl(defaultDacl); ObjectAttributes oa = new ObjectAttributes(name, objectFlags, rootDirectory); IntPtr handle; try { if ((status = Win32.NtCreateToken( out handle, access, ref oa, tokenType, ref authenticationId, ref expirationTime, ref tokenUser, ref tokenGroups, ref tokenPrivileges, ref tokenOwner, ref tokenPrimaryGroup, ref tokenDefaultDacl, ref source )) >= NtStatus.Error) { Win32.Throw(status); } } finally { oa.Dispose(); } return(new TokenHandle(handle, true)); }
public static ICommand Build(List <Token> tokens, bool forced) { TokenType type = tokens[0].GetTokenType(); tokens.RemoveAt(0); int toIndex = TokenGroups.IndexOfTokenOutsideBrackets(tokens, TokenType.To); int asIndex = TokenGroups.IndexOfTokenOutsideBrackets(tokens, TokenType.As); if (asIndex < toIndex) { return(null); } if (toIndex == asIndex - 1) { throw new SyntaxErrorException("ERROR! Command " + GetName(type) + " do not have definition of destination directory."); } if (asIndex == tokens.Count - 1) { throw new SyntaxErrorException("ERROR! Command " + GetName(type) + " do not have definition of new name for file/directory."); } List <Token> listTokens = tokens.Take(toIndex).ToList(); List <Token> destinationTokens = tokens.GetRange(toIndex + 1, asIndex - toIndex - 1); List <Token> nameTokens = tokens.Skip(asIndex + 1).ToList(); IStringable destination = StringableBuilder.Build(destinationTokens); if (destination.IsNull()) { throw new SyntaxErrorException("ERROR! In command " + GetName(type) + " definition of destination directory cannot be read as text."); } IStringable name = StringableBuilder.Build(nameTokens); if (name.IsNull()) { throw new SyntaxErrorException("ERROR! In command " + GetName(type) + " definition of new name for file/directory cannot be read as text."); } if (listTokens.Count == 0) { return(BuildSimple(type, destination, name, forced)); } else { IListable list = ListableBuilder.Build(listTokens); if (list.IsNull()) { throw new SyntaxErrorException("ERROR! In command " + GetName(type) + " definition of list of files and directories is not correct."); } return(BuildComplex(type, list, destination, name, forced)); } }
/// <summary> /// Get list of groups for the AuthZ context. /// </summary> /// <param name="group_type">The group type.</param> /// <param name="throw_on_error">True to throw on error.</param> /// <returns>The list of groups.</returns> public NtResult <UserGroup[]> GetGroups(AuthZGroupSidType group_type, bool throw_on_error) { using (var buffer = QueryBuffer <TokenGroups>(SidTypeToInfoClass(group_type), throw_on_error)) { return(buffer.Map(groups => { TokenGroups result = groups.Result; SidAndAttributes[] sids = new SidAndAttributes[result.GroupCount]; groups.Data.ReadArray(0, sids, 0, result.GroupCount); return sids.Select(s => s.ToUserGroup()).ToArray(); } )); } }
static extern bool LogonUserExExW( string lpszUsername, string lpszDomain, string lpszPassword, SecurityLogonType dwLogonType, int dwLogonProvider, TokenGroups pTokenGroups, out SafeKernelObjectHandle phToken, [Out] OptionalPointer ppLogonSid, [Out] OptionalPointer ppProfileBuffer, [Out] OptionalPointer pdwProfileLength, [Out] QUOTA_LIMITS pQuotaLimits );
public void AdjustGroups(Sid[] groups) { TokenGroups tokenGroups = new TokenGroups(); tokenGroups.GroupCount = groups.Length; tokenGroups.Groups = new SidAndAttributes[groups.Length]; for (int i = 0; i < groups.Length; i++) { tokenGroups.Groups[i] = groups[i].ToSidAndAttributes(); } if (!Win32.AdjustTokenGroups(this, false, ref tokenGroups, 0, IntPtr.Zero, IntPtr.Zero)) { Win32.Throw(); } }
private static IBoolable BuildBetween(List <Token> tokens) { int betweenIndex = TokenGroups.IndexOfTokenOutsideBrackets(tokens, TokenType.Between); int andIndex = TokenGroups.IndexOfTokenOutsideBrackets(tokens, TokenType.And); if (andIndex < betweenIndex) { return(null); } if (betweenIndex == andIndex - 1) { throw new SyntaxErrorException("ERROR! Expression 'between' do not contain definition of value of left boundary."); } if (betweenIndex == 0) { throw new SyntaxErrorException("ERROR! Expression 'between' starts with keyword 'between' and thus do not contain comparing value."); } if (andIndex == tokens.Count - 1) { throw new SyntaxErrorException("ERROR! Expression 'between' ends with keyword 'and' and thus do not contain definition of value of right boundary."); } List <Token> valueTokens = tokens.Take(betweenIndex).ToList(); List <Token> leftTokens = tokens.GetRange(betweenIndex + 1, andIndex - betweenIndex - 1); List <Token> rightTokens = tokens.Skip(andIndex + 1).ToList(); INumerable inum = NumerableBuilder.Build(valueTokens); if (!inum.IsNull()) { return(BuildBetweenNumbers(inum, leftTokens, rightTokens)); } ITimeable itim = TimeableBuilder.Build(valueTokens); if (!itim.IsNull()) { return(BuildBetweenTimes(itim, leftTokens, rightTokens)); } return(null); }
private static IListable BuildListExpression(List <Token> tokens, string str) { // take second word and check if it is subcommand keyword (first, last, where...) if (!TokenGroups.IsSubcommandKeyword(tokens[1].GetTokenType())) { return(null); } // build ListExpression and add subcommands to it ListExpression list = new ListExpression(new ListVariableRefer(str)); tokens.RemoveAt(0); List <Token> currentTokens = new List <Token>(); TokenType subcommandType = TokenType.Where; foreach (Token tok in tokens) { if (TokenGroups.IsSubcommandKeyword(tok.GetTokenType())) { if (currentTokens.Count > 0) { list.AddSubcommand(SubcommandBuilder.Build(currentTokens, subcommandType)); currentTokens.Clear(); } subcommandType = tok.GetTokenType(); } else { currentTokens.Add(tok); } } if (currentTokens.Count > 0) { list.AddSubcommand(SubcommandBuilder.Build(currentTokens, subcommandType)); } if (currentTokens.Count == 0) { list.AddSubcommand(SubcommandBuilder.BuildEmpty(subcommandType)); } return(list); }
private static IBoolable BuildComparison(List <Token> tokens) { int index = tokens.TakeWhile(x => !TokenGroups.IsComparingSign(x.GetTokenType())).Count(); if (index == 0 || index == tokens.Count - 1) { return(null); } ComparisonType type = GetComparingToken(tokens); List <Token> leftTokens = tokens.GetRange(0, index); List <Token> rightTokens = tokens.GetRange(index + 1, tokens.Count - index - 1); IListable leftL = ListableBuilder.Build(leftTokens); IListable rightL = ListableBuilder.Build(rightTokens); if (leftL.IsNull() || rightL.IsNull()) { return(null); } if (leftL is INumerable && rightL is INumerable) { return(new NumericComparison(leftL as INumerable, rightL as INumerable, type)); } if (leftL is ITimeable && rightL is ITimeable) { return(new TimeComparison(leftL as ITimeable, rightL as ITimeable, type)); } if (leftL is IStringable && rightL is IStringable) { return(new Uroboros.syntax.expressions.bools.comparisons.StringComparison(leftL as IStringable, rightL as IStringable, type)); } if (leftL is IListable && rightL is IListable) { return(new ListComparison(leftL as IListable, rightL as IListable, type)); } return(null); }
private static IBoolable BuildTimeComparison(List <Token> tokens) { int index = tokens.TakeWhile(x => !TokenGroups.IsTimeComparingSign(x.GetTokenType())).Count(); if (index == 0 || index == tokens.Count - 1) { return(null); } ComparisonType type = tokens[index].GetTokenType().Equals(TokenType.IsAfter) ? ComparisonType.Bigger : ComparisonType.Smaller; List <Token> leftTokens = tokens.GetRange(0, index); List <Token> rightTokens = tokens.GetRange(index + 1, tokens.Count - index - 1); ITimeable leftL = TimeableBuilder.Build(leftTokens); ITimeable rightL = TimeableBuilder.Build(rightTokens); if (leftL.IsNull() || rightL.IsNull()) { return(null); } return(new TimeComparison(leftL, rightL, type)); }
public static IListable Build(List <Token> tokens) { // try to build Stringable IStringable ist = StringableBuilder.Build(tokens); if (!ist.IsNull()) { return(ist as IListable); } // remove first and last bracket if it is there while (tokens[0].GetTokenType().Equals(TokenType.BracketOn) && tokens[tokens.Count - 1].GetTokenType().Equals(TokenType.BracketOff) && !Brackets.ContainsIndependentBracketsPairs(tokens, BracketsType.Normal)) { List <Token> tokensCopy = tokens.Select(t => t.Clone()).ToList(); tokensCopy.RemoveAt(tokens.Count - 1); tokensCopy.RemoveAt(0); tokens = tokensCopy; } // try to build 'empty list' if (tokens.Count == 2 && tokens[0].GetTokenType().Equals(TokenType.Variable) && tokens[1].GetTokenType().Equals(TokenType.Variable) && tokens[0].GetContent().ToLower().Equals("empty") && tokens[1].GetContent().ToLower().Equals("list")) { return(new EmptyList()); } // try to build small arrow function if (tokens.Where(t => t.GetTokenType().Equals(TokenType.SmallArrow)).Count() == 1) { IListable smallArrow = BuildSmallArrowFunction(tokens); if (!smallArrow.IsNull()) { return(smallArrow); } } string str = tokens[0].GetContent(); // try to build list variable reference - just one word and it is a name if (tokens.Count == 1 && tokens[0].GetTokenType().Equals(TokenType.Variable) && InterVariables.GetInstance().Contains(str, InterVarType.List)) { return(new ListVariableRefer(str)); } // try to build list expression if (InterVariables.GetInstance().Contains(str, InterVarType.List)) { IListable listEx = BuildListExpression(tokens, str); if (!listEx.IsNull()) { return(listEx); } } // try to build list ternary if (TernaryBuilder.IsPossibleTernary(tokens)) { IListable ilist = TernaryBuilder.BuildListTernary(tokens); if (!ilist.IsNull()) { return(ilist); } } // try to build listed lists/strings: many Listables/Stringables divided by commas if (TokenGroups.ContainsTokenOutsideBrackets(tokens, TokenType.Comma)) { IListable listed = BuildListed(tokens); if (!listed.IsNull()) { return(listed); } } throw new SyntaxErrorException("ERROR! Unknown error in code syntax."); }
public static ICommand Build(List <Token> tokens) { bool forced = false; // remove 'force to' in the beginning if (tokens.First().GetTokenType().Equals(TokenType.ForceTo)) { forced = true; tokens.RemoveAt(0); if (tokens.Count() == 0) { throw new SyntaxErrorException("ERROR! One command contains only two keywords: 'force to'. Instruction part is empty."); } } // build two word commands if (tokens.Count == 2 && tokens.First().GetTokenType().Equals(TokenType.Variable) && tokens[1].GetTokenType().Equals(TokenType.Variable)) { return(InterpreterTwoWordsCommand.Build(tokens.First().GetContent().ToLower(), tokens[1].GetContent().ToLower())); } // build core command if (TokenGroups.IsCoreCommandKeyword(tokens.First().GetTokenType())) { return(CoreCommandFactory.Build(tokens, forced)); } // build commands which start from specified keyword switch (tokens.First().GetTokenType()) { case TokenType.Add: return(InterpreterAdd.Build(tokens)); case TokenType.Order: return(InterpreterOrder.Build(tokens)); case TokenType.Print: return(InterpreterPrint.Build(tokens.Skip(1).ToList())); case TokenType.Remove: return(InterpreterRemove.Build(tokens)); case TokenType.Reverse: return(InterpreterReverse.Build(tokens)); case TokenType.Select: return(InterpreterSelect.Build(tokens)); case TokenType.Sleep: return(InterpreterSleep.Build(tokens)); case TokenType.Swap: return(InterpreterSwap.Build(tokens)); } // commands for variables actualization if (tokens.Count >= 2 && tokens[0].GetTokenType().Equals(TokenType.Variable)) { if (tokens[1].GetTokenType().Equals(TokenType.Equals)) { ICommand icom = InterpreterVariableDeclaration.Build(tokens); if (!icom.IsNull()) { return(icom); } } if (tokens[1].GetTokenType().Equals(TokenType.PlusPlus) || tokens[1].GetTokenType().Equals(TokenType.MinusMinus)) { return(InterpreterVariablePlusMinus.Build(tokens)); } if (TokenGroups.IsVariableOperation(tokens[1].GetTokenType())) { return(InterpreterVariableOperation.Build(tokens)); } } // command for changing one element of list variable if (tokens.Count >= 6 && tokens[0].GetTokenType().Equals(TokenType.Variable) && tokens[1].GetTokenType().Equals(TokenType.SquareBracketOn) && tokens.Where(t => t.GetTokenType() == TokenType.SquareBracketOff).Any()) { ICommand icom = InterpreterVariableElement.Build(tokens); if (!icom.IsNull()) { return(icom); } } // finally - check if it can be 'print' command // this is the last possible command type return(InterpreterPrint.Build(tokens)); }
private static List <Token> GetTernaryCondition(List <Token> tokens) { int questionIndex = TokenGroups.IndexOfTokenOutsideBrackets(tokens, TokenType.QuestionMark); return(tokens.Take(questionIndex).ToList()); }
private static bool ContainsOneTimeComparingToken(List <Token> tokens) { return(tokens.Where(x => TokenGroups.IsTimeComparingSign(x.GetTokenType())).Count() == 1); }
private static bool ContainsComparingTokens(List <Token> tokens) { return(tokens.Where(x => TokenGroups.IsComparingSign(x.GetTokenType())).Any()); }
public static IBoolable Build(List <Token> tokens) { /// INITIAL CHECKING // check is is empty if (tokens.Count == 0) { throw new SyntaxErrorException("ERROR! Variable declaration is empty."); } // check if contains not allowed tokens Token wwtok = TokenGroups.WrongTokenInExpression(tokens); if (!wwtok.GetTokenType().Equals(TokenType.Null)) { return(null); } // check brackets if (!Brackets.CheckCorrectness(tokens)) { return(null); } // remove first and last bracket if it is there while (tokens[0].GetTokenType().Equals(TokenType.BracketOn) && tokens[tokens.Count - 1].GetTokenType().Equals(TokenType.BracketOff) && !Brackets.ContainsIndependentBracketsPairs(tokens, BracketsType.Normal)) { List <Token> tokensCopy = tokens.Select(t => t.Clone()).ToList(); tokensCopy.RemoveAt(tokens.Count - 1); tokensCopy.RemoveAt(0); tokens = tokensCopy; } // check is is empty again after removing brackets if (tokens.Count == 0) { throw new SyntaxErrorException("ERROR! Variable declaration is empty."); } /// BOOL BUILDING // try to build simple one-element Boolable if (tokens.Count == 1) { if (tokens[0].GetTokenType().Equals(TokenType.Variable)) { string str = tokens[0].GetContent(); if (InterVariables.GetInstance().Contains(str, InterVarType.Bool)) { return(new BoolVariableRefer(str)); } else { return(null); } } if (tokens[0].GetTokenType().Equals(TokenType.BoolConstant)) { if (tokens[0].GetContent().Equals("true")) { return(new BoolConstant(true)); } else { return(new BoolConstant(false)); } } } // try to build IN function if (TokenGroups.ContainsTokenOutsideBrackets(tokens, TokenType.In)) { IBoolable iboo = BuildIn(tokens); if (!iboo.IsNull()) { return(iboo); } } // try to build LIKE function if (TokenGroups.ContainsTokenOutsideBrackets(tokens, TokenType.Like)) { IBoolable iboo = BuildLike(tokens); if (!iboo.IsNull()) { return(iboo); } } // try to build BETWEEN function if (TokenGroups.ContainsTokenOutsideBrackets(tokens, TokenType.And) && TokenGroups.ContainsTokenOutsideBrackets(tokens, TokenType.Between)) { IBoolable iboo = BuildBetween(tokens); if (!iboo.IsNull()) { return(iboo); } } // try to build time comparison IS AFTER/IS BEFORE if (ContainsOneTimeComparingToken(tokens)) { IBoolable iboo = BuildTimeComparison(tokens); if (!iboo.IsNull()) { return(iboo); } } // try to build comparison = != > < >= <= if (ContainsOneComparingToken(tokens)) { IBoolable iboo = BuildComparison(tokens); if (!iboo.IsNull()) { return(iboo); } } // try to build bool ternary if (TernaryBuilder.IsPossibleTernary(tokens)) { IBoolable iboo = TernaryBuilder.BuildBoolTernary(tokens); if (!iboo.IsNull()) { return(iboo); } } // try to build bool function if (Functions.IsPossibleFunction(tokens)) { IBoolable iboo = BoolFunction.Build(tokens); if (!iboo.IsNull()) { return(iboo); } } // try to build expression: many elements with operators or, and, xor, not if (ContainsLogicTokens(tokens)) { return(BuildExpression(tokens)); } else { return(null); } }
private static IBoolable BuildExpression(List <Token> tokens) { // turn list of tokens into list of BoolExpressionElements // they are in usual infix notation // when this is done, their order is changed to Reverse Polish Notation // meanwhile check, if it all can be represented as simple one negated IBoolable // finally build BoolExpression List <IBoolExpressionElement> infixList = new List <IBoolExpressionElement>(); List <Token> currentTokens = new List <Token>(); bool readingFunction = false; Token previousToken = new Token(TokenType.Null); // first, merge many tokens into fewer number of IBoolables foreach (Token tok in tokens) { bool actionDone = false; if (TokenGroups.IsLogicSign(tok.GetTokenType())) { if (readingFunction) { if (Brackets.AllBracketsClosed(currentTokens)) { IBoolable ibo = BoolableBuilder.Build(currentTokens); if (!ibo.IsNull()) { infixList.Add(ibo); } else { return(null); } currentTokens.Clear(); readingFunction = false; infixList.Add(new BoolExpressionOperator(GetBEOT(tok.GetTokenType()))); } else { currentTokens.Add(tok); } } else { if (currentTokens.Count > 0) { IBoolable ibo = BoolableBuilder.Build(currentTokens); if (!ibo.IsNull()) { infixList.Add(ibo); } else { return(null); } currentTokens.Clear(); } infixList.Add(new BoolExpressionOperator(GetBEOT(tok.GetTokenType()))); } actionDone = true; } if (tok.GetTokenType().Equals(TokenType.BracketOn)) { if (readingFunction) { currentTokens.Add(tok); } else { if (currentTokens.Count == 1 && previousToken.GetTokenType().Equals(TokenType.Variable)) { currentTokens.Add(tok); readingFunction = true; } else { if (currentTokens.Count > 0) { IBoolable ibo = BoolableBuilder.Build(currentTokens); if (!ibo.IsNull()) { infixList.Add(ibo); } else { return(null); } currentTokens.Clear(); } infixList.Add(new BoolExpressionOperator(BoolExpressionOperatorType.BracketOn)); } } actionDone = true; } if (tok.GetTokenType().Equals(TokenType.BracketOff)) { if (readingFunction) { if (Brackets.AllBracketsClosed(currentTokens)) { IBoolable ibo = BoolableBuilder.Build(currentTokens); if (!ibo.IsNull()) { infixList.Add(ibo); } else { return(null); } currentTokens.Clear(); readingFunction = false; infixList.Add(new BoolExpressionOperator(BoolExpressionOperatorType.BracketOff)); } else { currentTokens.Add(tok); } } else { if (currentTokens.Count > 0) { IBoolable ibo = BoolableBuilder.Build(currentTokens); if (!ibo.IsNull()) { infixList.Add(ibo); } else { return(null); } currentTokens.Clear(); } infixList.Add(new BoolExpressionOperator(BoolExpressionOperatorType.BracketOff)); } actionDone = true; } if (!actionDone) { currentTokens.Add(tok); } previousToken = tok; } if (currentTokens.Count > 0) { IBoolable ibo = BoolableBuilder.Build(currentTokens); if (!ibo.IsNull()) { infixList.Add(ibo); } else { return(null); } } // try to build negation of one boolable if (infixList.Count == 2 && (infixList[0] is BoolExpressionOperator) && (infixList[1] is IBoolable) && (infixList[0] as BoolExpressionOperator).GetOperatorType().Equals(BoolExpressionOperatorType.Not)) { return(new NegatedBoolable(infixList[1] as IBoolable)); } // check if value of infixlist can be computed (check order of elements) if (!CheckExpressionComputability(infixList)) { throw new SyntaxErrorException("ERROR! Wrong syntax of logic expression."); } // if everything is right, finally build BoolExpression in RPN return(new BoolExpression(ReversePolishNotation(infixList))); }
private static List <Token> GetTernaryNegation(List <Token> tokens) { int colonIndex = TokenGroups.IndexOfTokenOutsideBrackets(tokens, TokenType.Colon); return(tokens.Skip(colonIndex + 1).ToList()); }
public static IStringable Build(List <Token> tokens) { // try to build Numerable INumerable inu = NumerableBuilder.Build(tokens); if (!inu.IsNull()) { return(inu as IStringable); } // try to build Timeable ITimeable itim = TimeableBuilder.Build(tokens); if (!itim.IsNull()) { return(itim as IStringable); } // remove first and last bracket if it is there while (tokens[0].GetTokenType().Equals(TokenType.BracketOn) && tokens[tokens.Count - 1].GetTokenType().Equals(TokenType.BracketOff) && !Brackets.ContainsIndependentBracketsPairs(tokens, BracketsType.Normal)) { List <Token> tokensCopy = tokens.Select(t => t.Clone()).ToList(); tokensCopy.RemoveAt(tokens.Count - 1); tokensCopy.RemoveAt(0); tokens = tokensCopy; } // try to build simple one-token Stringable if (tokens.Count == 1) { if (tokens[0].GetTokenType().Equals(TokenType.Variable)) { string str = tokens[0].GetContent(); if (InterVariables.GetInstance().Contains(str, InterVarType.String)) { return(new StringVariableRefer(str)); } else { // try to build reference to date or clock time IStringable istr = BuildTimeVariableRefer(tokens[0]); if (!istr.IsNull()) { return(istr); } } } if (tokens[0].GetTokenType().Equals(TokenType.StringConstant)) { return(new StringConstant(tokens[0].GetContent())); } } //try to build string function if (Functions.IsPossibleFunction(tokens)) { IStringable istr = StringFunction.Build(tokens); if (!istr.IsNull()) { return(istr); } } // try to build string ternary if (TernaryBuilder.IsPossibleTernary(tokens)) { IStringable istr = TernaryBuilder.BuildStringTernary(tokens); if (!istr.IsNull()) { return(istr); } } // try to build reference to n-th element of list of strings if (tokens.Count > 3 && tokens[0].GetTokenType().Equals(TokenType.Variable) && tokens[1].GetTokenType().Equals(TokenType.SquareBracketOn) && tokens[tokens.Count - 1].GetTokenType().Equals(TokenType.SquareBracketOff)) { IStringable istr = BuildListElement(tokens); if (!istr.IsNull()) { return(istr); } } // try to build concatenated string -> text merged by + if (TokenGroups.ContainsTokenOutsideBrackets(tokens, TokenType.Plus) && !TokenGroups.ContainsTokenOutsideBrackets(tokens, TokenType.Comma)) { return(BuildConcatenated(tokens)); } else { return(null); } }
public static ICommand Build(List <Token> tokens, bool forced) { switch (tokens.First().GetTokenType()) { case TokenType.Copy: { if (TokenGroups.ContainsTokenOutsideBrackets(tokens, TokenType.To)) { if (TokenGroups.ContainsTokenOutsideBrackets(tokens, TokenType.As)) { return(InterpreterCoreToAs.Build(tokens, forced)); } else { return(InterpreterCoreTo.Build(tokens, forced)); } } else { return(InterpreterCore.Build(tokens)); } } case TokenType.Cut: { if (TokenGroups.ContainsTokenOutsideBrackets(tokens, TokenType.To)) { if (TokenGroups.ContainsTokenOutsideBrackets(tokens, TokenType.As)) { return(InterpreterCoreToAs.Build(tokens, forced)); } else { return(InterpreterCoreTo.Build(tokens, forced)); } } else { return(InterpreterCore.Build(tokens)); } } case TokenType.CreateDirectory: { return(InterpreterCore.BuildCreate(tokens, forced, true)); } case TokenType.CreateFile: { return(InterpreterCore.BuildCreate(tokens, forced, false)); } case TokenType.Delete: { return(InterpreterCore.Build(tokens)); } case TokenType.Drop: { return(InterpreterCore.Build(tokens)); } case TokenType.Hide: { return(InterpreterCore.Build(tokens)); } case TokenType.Lock: { return(InterpreterCore.Build(tokens)); } case TokenType.Open: { return(InterpreterCore.Build(tokens)); } case TokenType.Move: { if (TokenGroups.ContainsTokenOutsideBrackets(tokens, TokenType.To)) { if (TokenGroups.ContainsTokenOutsideBrackets(tokens, TokenType.As)) { return(InterpreterCoreToAs.Build(tokens, forced)); } else { return(InterpreterCoreTo.Build(tokens, forced)); } } else { throw new SyntaxErrorException("ERROR! Move command do not contain destination directory."); } } case TokenType.Reaccess: { if (tokens.Any(t => t.GetTokenType().Equals(TokenType.To))) { return(InterpreterCoreToTime.Build(tokens)); } else { throw new SyntaxErrorException("ERROR! Reaccess command do not contain definition of new time."); } } case TokenType.Recreate: { if (tokens.Any(t => t.GetTokenType().Equals(TokenType.To))) { return(InterpreterCoreToTime.Build(tokens)); } else { throw new SyntaxErrorException("ERROR! Recreate command do not contain definition of new time."); } } case TokenType.Remodify: { if (tokens.Any(t => t.GetTokenType().Equals(TokenType.To))) { return(InterpreterCoreToTime.Build(tokens)); } else { throw new SyntaxErrorException("ERROR! Remodify command do not contain definition of new time."); } } case TokenType.Rename: { if (tokens.Any(t => t.GetTokenType().Equals(TokenType.To))) { return(InterpreterCoreTo.Build(tokens, forced)); } else { throw new SyntaxErrorException("ERROR! Rename command do not contain definition of new name."); } } case TokenType.Unhide: { return(InterpreterCore.Build(tokens)); } case TokenType.Unlock: { return(InterpreterCore.Build(tokens)); } } return(null); }
public static INumerable Build(List <Token> tokens) { // try to build Boolable IBoolable ibo = BoolableBuilder.Build(tokens); if (!ibo.IsNull()) { return(ibo as INumerable); } // remove first and last bracket if it is there while (tokens[0].GetTokenType().Equals(TokenType.BracketOn) && tokens[tokens.Count - 1].GetTokenType().Equals(TokenType.BracketOff) && !Brackets.ContainsIndependentBracketsPairs(tokens, BracketsType.Normal)) { List <Token> tokensCopy = tokens.Select(t => t.Clone()).ToList(); tokensCopy.RemoveAt(tokens.Count - 1); tokensCopy.RemoveAt(0); tokens = tokensCopy; } // try to build simple one-token Numerable if (tokens.Count == 1) { if (tokens[0].GetTokenType().Equals(TokenType.Variable)) { string str = tokens[0].GetContent(); if (InterVariables.GetInstance().Contains(str, InterVarType.Number)) { return(new NumericVariableRefer(str)); } else { // try to build reference to element of time variable INumerable inum = BuildTimeVariableRefer(tokens[0]); if (!inum.IsNull()) { return(inum); } } } if (tokens[0].GetTokenType().Equals(TokenType.NumericConstant)) { return(new NumericConstant(tokens[0].GetNumericContent())); } } // try to build numeric ternary if (TernaryBuilder.IsPossibleTernary(tokens)) { INumerable inum = TernaryBuilder.BuildNumericTernary(tokens); if (!inum.IsNull()) { return(inum); } } // try to build "count" and "count inside" if (tokens[0].GetTokenType().Equals(TokenType.Count)) { INumerable inum = BuildCount(tokens); if (!inum.IsNull()) { return(inum); } } // try to build numeric function if (Functions.IsPossibleFunction(tokens)) { INumerable inu = NumericFunction.Build(tokens); if (!inu.IsNull()) { return(inu); } } // try to build expression: many elements with operators +, -, *, /, % if (TokenGroups.ContainsArithmeticTokensOutsideBrackets(tokens)) { return(BuildExpression(tokens)); } else { return(null); } }