public NodeState(ParseState state, int level) { Level = level; Parents = state.Parents; Root = state.Root; FilePath = state.FilePath; }
public ParseState this[ParseState current, BnfTerm symbol] { get { Dictionary<BnfTerm, ParseState> transition; if (!transitionRules.TryGetValue(current, out transition)) { if (!allStates.Contains(current)) throw new ParseException("No such state"); else return null; } ParseState next; if (!transition.TryGetValue(symbol, out next)) { HashSet<Terminal> expected = new HashSet<Terminal>(); foreach (var item in current) { if (item.Position < item.Production.Body.Length) expected.UnionWith(grammar.GetFirstSet(item.Production.Body[item.Position])); } throw new ParseException("Invalid symbol " + symbol.ToString() + " expected one of [" + String.Join(", ", expected.Select(a => a.Name)) + "]"); } return next; } }
/// <summary> /// Initializes a new instance of the <see cref="CborMapReader"/> class. /// </summary> /// <param name="reader"> /// The reader. /// </param> /// <param name="size"> /// The size. /// </param> /// <param name="targeType"> /// The targe type. /// </param> public CborMapReader(CborReader reader, int size, Type targeType) : base(reader) { this.currentSize = size; this.state = ParseState.FIRST_KEY; this.dictionary = (IDictionary)Activator.CreateInstance(targeType); }
public bool GetNextToken(TokenInfo tokenInfo, ref ParseState state) { bool foundToken = false; int index = _offset; if (index >= _source.Length) return false; int endIndex = -1; switch (state) { case ParseState.InSingleQuotes: state = HandleSingleQuotes(out endIndex, ref foundToken); break; case ParseState.InDoubleQuotes: state = HandleDoubleQuotes(out endIndex, ref foundToken); break; case ParseState.InSingleQuoteHeredoc: state = HandleHeredoc('\'', state, out endIndex); break; case ParseState.InDoubleQuoteHeredoc: state = HandleHeredoc('"', state, out endIndex); break; case ParseState.InBlockComment: state = HandleBlockComment(out endIndex); break; case ParseState.InText: state = HandleToken(ref foundToken, ref endIndex); break; } tokenInfo.EndIndex = endIndex; _offset = endIndex + 1; return foundToken; }
public static Node Parse(ParseState parsestate) { Operator @operator = parsestate.CurrentOperator; switch (@operator) { case Operator.Equals: case Operator.NotEquals: case Operator.Greater: case Operator.GreaterEquals: case Operator.Lesser: case Operator.LesserEquals: ++parsestate.TokenIndex; break; default: return null; } Node child1 = parsestate.BuildNode(true); if (child1 == null) return null; if (parsestate.CurrentSymbol != Symbol.Comma) return null; ++parsestate.TokenIndex; Node child2 = parsestate.BuildNode(true); if (child2 == null) return null; parsestate.BaseNode.Children.Add(child1); parsestate.BaseNode.Children.Add(child2); return parsestate.BaseNode; }
public static Node Parse(ParseState parsestate) { if (parsestate.CurrentSymbol != Symbol.LeftParen) return null; ++parsestate.TokenIndex; Node c1 = parsestate.BuildNode(true); if (c1 == null) return null; parsestate.BaseNode.Children.Add(c1); if (parsestate.CurrentSymbol != Symbol.Comma) return null; ++parsestate.TokenIndex; Node c2 = parsestate.BuildNode(true); if (c2 == null) return null; parsestate.BaseNode.Children.Add(c2); if (parsestate.CurrentSymbol != Symbol.Comma) return null; ++parsestate.TokenIndex; Node c3 = parsestate.BuildNode(true); if (c3 == null) return null; parsestate.BaseNode.Children.Add(c3); if (parsestate.CurrentSymbol != Symbol.RightParen) return null; ++parsestate.TokenIndex; return parsestate.BaseNode; }
public static Node Parse(ParseState parsestate) { Operator @operator = parsestate.CurrentOperator; if (@operator != Operator.Equals && @operator != Operator.NotEquals) return null; parsestate.BaseNode.Arguments.Add(@operator); ++parsestate.TokenIndex; AttackStateType ast = parsestate.ConvertCurrentToken<AttackStateType>(); if (ast == AttackStateType.None) return null; parsestate.BaseNode.Arguments.Add(ast); ++parsestate.TokenIndex; List<Combat.HitType> hittypes = new List<Combat.HitType>(); while (true) { if (parsestate.CurrentSymbol != Symbol.Comma) break; ++parsestate.TokenIndex; Combat.HitType? hittype = parsestate.ConvertCurrentToken<Combat.HitType?>(); if (hittype == null) { --parsestate.TokenIndex; break; } hittypes.Add(hittype.Value); ++parsestate.TokenIndex; } parsestate.BaseNode.Arguments.Add(hittypes.ToArray()); return parsestate.BaseNode; }
public ErrorState(ParseState state, int level, string message) { Level = level; Parents = state.Parents; Root = state.Root; FilePath = state.FilePath; Message = message; }
/// <summary> /// Read buffer data to Data property and initialize buffer. /// If response has next data,return true. /// </summary> /// <param name="size"></param> /// <returns>If response has next data,return true</returns> protected override Boolean ParseBuffer(Int32 size) { Byte[] bb = this.Buffer; Int32 tagIndex = 0; for (int i = 0; i < size; i++) { this.Stream.WriteByte(bb[i]); if (_State == ParseState.TagValidating) { if (bb[i] == _TagBytes[tagIndex]) { tagIndex = tagIndex + 1; if (_TagBytes.Length == tagIndex) { _State = ParseState.LastLine; } } else { _State = ParseState.MultiLine; } } else if (_State == ParseState.MultiLine) { if (bb[i] == AsciiCharCode.CarriageReturn.GetNumber()) { _State = ParseState.CarriageReturn; } } else if (_State == ParseState.CarriageReturn) { if (bb[i] == AsciiCharCode.LineFeed.GetNumber()) { tagIndex = 0; _State = ParseState.TagValidating; } else { throw new DataTransferContextException(this); } } else if (_State == ParseState.LastLine) { if (bb[i] == AsciiCharCode.CarriageReturn.GetNumber()) { _State = ParseState.LastLineCarriageReturn; } } else if (_State == ParseState.LastLineCarriageReturn) { if (bb[i] == AsciiCharCode.LineFeed.GetNumber()) { return false; } else { throw new DataTransferContextException(this); } } bb[i] = 0; } return true; }
public override void load() { base.load(); string[] t_loadedFile = File.ReadAllLines("Content//wtf//settings.ini"); foreach (string t_currentLine in t_loadedFile) { if (t_currentLine.Length > 2 && t_currentLine.First() == '[' && t_currentLine.Last() == ']') { if (t_currentLine.Equals("[Graphics]")) { m_currentParse = ParseState.Settings; } } switch (m_currentParse) { case ParseState.Settings: string[] t_setting = t_currentLine.Split('='); if (t_setting[0].Equals("ScreenWidth")) { Game.getInstance().m_graphics.PreferredBackBufferWidth = int.Parse(t_setting[1]); } else if (t_setting[0].Equals("ScreenHeight")) { Game.getInstance().m_graphics.PreferredBackBufferHeight = int.Parse(t_setting[1]); Game.getInstance().m_camera.setZoom(Game.getInstance().getResolution().Y / 720); } else if (t_setting[0].Equals("Fullscreen")) { Game.getInstance().m_graphics.IsFullScreen = bool.Parse(t_setting[1]); } break; } Game.getInstance().m_graphics.ApplyChanges(); } m_levelText = new Text(new Vector2(405, 80), "New Level:", "VerdanaBold", Color.White, false); m_newLevelName = new TextField(new Vector2(400, 100), 200, 32, true, true, true, 20); m_buttons.Add(m_btnTFAccept = new Button("btn_textfield_accept", new Vector2(600, 100))); m_btnTFAccept.m_clickEvent += new Button.clickDelegate(createNewLevel); try { m_levelList = Directory.GetFiles("Content//levels//"); } catch (DirectoryNotFoundException) { System.IO.Directory.CreateDirectory("Content//levels//"); return; } int t_count = 0; foreach (string t_level in m_levelList) { string[] t_splitPath = Regex.Split(t_level, "/"); Button t_levelButton = new Button("btn_test_empty", "btn_test_empty", "btn_test_empty", "btn_test_empty", new Vector2(20, 60 * t_count + 20), t_splitPath[t_splitPath.Length - 1], "VerdanaBold", Color.Black, new Vector2(10, 10)); t_levelButton.m_clickEvent += new Button.clickDelegate(startLevelClick); m_buttons.Add(t_levelButton); t_count++; } }
/// <summary> /// Initializes a new instance of the <see cref="CborObjectReader"/> class. /// </summary> /// <param name="reader"> /// The reader. /// </param> /// <param name="innerTemplate"> /// The inner template. /// </param> /// <param name="size"> /// The size. /// </param> public CborObjectReader(CborReader reader, CborTypeTemplate innerTemplate, int size) : base(reader) { this.template = innerTemplate; this.currentSize = size; this.obj = Activator.CreateInstance(this.template.type); this.state = ParseState.KEY; this.currentKey = null; }
protected override ParserAction Action(ParseState state, Token token, out Production production, out ParseState nextState) { var lr0 = base.Action(state, token, out production, out nextState); if (token != null && lr0 == ParserAction.Reduce && !Grammar.GetFollowSet(production.Head).Contains(token.Terminal)) return ParserAction.Error; return lr0; }
// // As required by IConfigurationSectionHandler // public object Create(object parent, object configurationContext, XmlNode section) { // if called through client config don't even load HttpRuntime if (!HandlerBase.IsServerConfiguration(configurationContext)) { return null; } ParseState parseState = new ParseState(); parseState.SectionName = section.Name; // the rule is going to be the previous rule followed by a list containing the new rules parseState.Evaluator = new HttpCapabilitiesDefaultProvider((HttpCapabilitiesDefaultProvider)parent); int userAgentCacheKeyLength = 0; // Get the useragent string cachekey length if (parent != null) { userAgentCacheKeyLength = ((HttpCapabilitiesDefaultProvider)parent).UserAgentCacheKeyLength; } HandlerBase.GetAndRemovePositiveIntegerAttribute(section, "userAgentCacheKeyLength", ref userAgentCacheKeyLength); if (userAgentCacheKeyLength == 0) { userAgentCacheKeyLength = _defaultUserAgentCacheKeyLength; } parseState.Evaluator.UserAgentCacheKeyLength = userAgentCacheKeyLength; string browserCapabilitiesProviderType = null; if (parent != null) { browserCapabilitiesProviderType = ((HttpCapabilitiesDefaultProvider)parent).BrowserCapabilitiesProviderType; } HandlerBase.GetAndRemoveNonEmptyStringAttribute(section, "provider", ref browserCapabilitiesProviderType); parseState.Evaluator.BrowserCapabilitiesProviderType = browserCapabilitiesProviderType; // check for random attributes HandlerBase.CheckForUnrecognizedAttributes(section); // iterate through XML section in order and apply the directives ArrayList sublist; sublist = RuleListFromElement(parseState, section, true); if (sublist.Count > 0) { parseState.RuleList.Add(new CapabilitiesSection(CapabilitiesRule.Filter, null, null, sublist)); } if (parseState.FileList.Count > 0) { parseState.IsExternalFile = true; ResolveFiles(parseState, configurationContext); } // Add the new rules parseState.Evaluator.AddRuleList(parseState.RuleList); return parseState.Evaluator; }
public void ParseSource(IEnumerable<char> chars) { if (_state != ParseState.BeginLine) throw new NotImplementedException("unexpected state: " + _state.ToString()); foreach (char ch in chars) _state = AppendChar(ch); EndChars(); }
protected override IFieldMapper Parse(Func<ProcessingScope, DateTime?> selector, string defaultName, JobParser parser, ParseState state) { return new TimeDimension( state.AffixName(state.TryGet("Name", defaultName + "Time")), selector, inlineFields: state.TryGet<bool>("Inline"), cultureInfo: parser.DefaultLanguage.CultureInfo, detailLevel: state.TryGet("Resolution", TimeDetailLevel.Hour, true)); }
public static Node Parse(ParseState parsestate) { Axis axis = parsestate.ConvertCurrentToken<Axis>(); if (axis == Axis.None) return null; ++parsestate.TokenIndex; parsestate.BaseNode.Arguments.Add(axis); return parsestate.BaseNode; }
public static Node Parse(ParseState parsestate) { if (parsestate.CurrentSymbol != Symbol.Comma) return null; ++parsestate.TokenIndex; Node child = parsestate.BuildNode(false); if (child == null) return null; parsestate.BaseNode.Children.Add(child); return parsestate.BaseNode; }
public static Node Parse(ParseState parsestate) { Node node = parsestate.BuildParenNumberNode(true); if (node != null) { return node; } else { parsestate.BaseNode.Children.Add(Node.NegativeOneNode); return parsestate.BaseNode; } }
protected override IFieldMapper Parse(Func<ProcessingScope, DateTime?> selector, string defaultName, JobParser parser, ParseState state) { return new DateDimension( state.AffixName(state.TryGet("Name", defaultName)), selector, useDateForKey: state.TryGet("DateKey", true), inlineFields: state.TryGet("Inline", false), cultureInfo: parser.DefaultLanguage.CultureInfo, key: state.TryGet("Key", false), detailLevel: state.TryGet("Resolution", DateDetailLevel.Date, true)); }
public static Node Parse(ParseState parsestate) { Operator @operator = parsestate.CurrentOperator; if (@operator != Operator.Equals && @operator != Operator.NotEquals) return null; ++parsestate.TokenIndex; String text = parsestate.CurrentUnknown; if (text == null) return null; ++parsestate.TokenIndex; parsestate.BaseNode.Arguments.Add(@operator); parsestate.BaseNode.Arguments.Add(text); return parsestate.BaseNode; }
public static Node Parse(ParseState state) { Operator @operator = state.CurrentOperator; if (@operator != Operator.Equals && @operator != Operator.NotEquals) return null; ++state.TokenIndex; String text = state.CurrentText; if (text == null) return null; ++state.TokenIndex; state.BaseNode.Arguments.Add(@operator); state.BaseNode.Arguments.Add(text); return state.BaseNode; }
/// <summary> /// Generates a random tree. /// </summary> /// <returns>Generated tree</returns> public Trees.Absyn.Tree Gen() { this.ps = new ParseState(this.concrete); this.ran = new Random(); var pre = this.ps.Predict(); while (pre.Count != 0) { this.ps.Next(pre[this.ran.Next(pre.Count - 1)]); pre = this.ps.Predict(); } var list = this.ps.GetTrees(); return list.Count == 0 ? this.Gen() : list[0]; }
public HttpListenerRequestStream(HttpConnectionState connectionState, long contentLength, bool readChunked) { #if DEBUG if (HttpTraceHelper.InternalLog.TraceVerbose) { HttpTraceHelper.WriteLine("ListenerRequestStream#" + HttpTraceHelper.HashString(this) + "::.ctor() contentLength:" + contentLength.ToString() + " readChunked:" + readChunked.ToString()); } #endif m_ConnectionState = connectionState; m_ContentLength = contentLength; m_LeftToRead = contentLength; m_ChunkParserState = ParseState.ChunkSize; m_ReadChunked = readChunked; m_MoreToRead = m_ConnectionState.Request.Uploading; }
public static Node Parse(ParseState state) { if (state.CurrentSymbol != Symbol.LeftParen) return null; ++state.TokenIndex; if (state.CurrentUnknown == null) return null; xnaMugen.Assertion assert = state.ConvertCurrentToken<xnaMugen.Assertion>(); state.BaseNode.Arguments.Add(assert); ++state.TokenIndex; if (state.CurrentSymbol != Symbol.RightParen) return null; ++state.TokenIndex; return state.BaseNode; }
public static Node Parse(ParseState parsestate) { Operator @operator = parsestate.CurrentOperator; if (@operator != Operator.Equals && @operator != Operator.NotEquals) return null; ++parsestate.TokenIndex; xnaMugen.Physics physics = parsestate.ConvertCurrentToken<xnaMugen.Physics>(); if (physics == xnaMugen.Physics.Unchanged || physics == xnaMugen.Physics.None) return null; ++parsestate.TokenIndex; parsestate.BaseNode.Arguments.Add(@operator); parsestate.BaseNode.Arguments.Add(physics); return parsestate.BaseNode; }
public HttpListenerRequestStream(HttpConnectionState connectionState, long contentLength, bool readChunked) { #if DEBUG if (HttpTraceHelper.InternalLog.TraceVerbose) { HttpTraceHelper.WriteLine("ListenerRequestStream#" + HttpTraceHelper.HashString(this) + "::.ctor() contentLength:" + contentLength + " readChunked:" + readChunked); } #endif _connectionState = connectionState; _contentLength = contentLength; _leftToRead = contentLength; _chunkParserState = ParseState.ChunkSize; _readChunked = readChunked; DataAvailable = _connectionState.Request.Uploading; }
public static Node Parse(ParseState parsestate) { Operator @operator = parsestate.CurrentOperator; if (@operator != Operator.Equals && @operator != Operator.NotEquals) return null; ++parsestate.TokenIndex; xnaMugen.MoveType movetype = parsestate.ConvertCurrentToken<xnaMugen.MoveType>(); if (movetype == xnaMugen.MoveType.Unchanged || movetype == xnaMugen.MoveType.None) return null; ++parsestate.TokenIndex; parsestate.BaseNode.Arguments.Add(@operator); parsestate.BaseNode.Arguments.Add(movetype); return parsestate.BaseNode; }
public HttpConnectionState(Socket socket, int bufferSize, HttpWebListener httpWebListener) { #if DEBUG if (HttpTraceHelper.InternalLog.TraceVerbose) { HttpTraceHelper.WriteLine("ConnectionState#" + HttpTraceHelper.HashString(this) + "::ctor() New client connected from: " + socket.RemoteEndPoint.ToString()); } #endif Interlocked.Increment(ref m_ActiveConnections); m_HttpWebListener = httpWebListener; m_Socket = socket; m_Buffer = new byte[bufferSize]; m_ParserState = ParseState.None; m_EofOffset = 0; m_ParsedOffset = 0; }
protected override ParserAction Action(ParseState state, Token token, out Production production, out ParseState nextState) { bool shift = false; bool reduce = false; if (token == null && state.AcceptingState) { production = default(Production); nextState = null; return ParserAction.Accept; } if (token != null) { nextState = automaton[state, token.Terminal]; if (nextState != null) { production = default(Production); shift = true; } } else nextState = null; if (state.Count() == 1 && state.First().Position == state.First().Production.Body.Length) { nextState = null; production = state.First().Production; reduce = true; } else production = default(Production); if (shift && reduce) throw new ParseException(String.Format("Shift/Reduce conflict between shift {0} and reduce {1}", nextState, production)); else if (shift) return ParserAction.Shift; else if (reduce) return ParserAction.Reduce; else if (token == null) throw new ParseException("Unexpected EOF"); throw new ParseException(String.Format("Unexpected {0} at line {1} column {2}", token.Value, token.Line, token.Column)); }
public static void loadSettings(string a_file) { string[] l_loadedFile = File.ReadAllLines("Settings//" + a_file + ".ini"); foreach (string l_currentLine in l_loadedFile) { if (l_currentLine.Length > 2 && l_currentLine.First() == '[' && l_currentLine.Last() == ']') { if (l_currentLine.Equals("[Input]")) { m_currentParse = ParseState.Input; } else if (l_currentLine.Equals("[Graphics]")) { m_currentParse = ParseState.Graphics; } } switch (m_currentParse) { case ParseState.Input: string[] l_input = l_currentLine.Split('='); break; case ParseState.Graphics: string[] l_setting = l_currentLine.Split('='); if (l_setting[0].Equals("ScreenWidth")) { Game.getInstance().m_graphics.PreferredBackBufferWidth = int.Parse(l_setting[1]); } else if (l_setting[0].Equals("ScreenHeight")) { Game.getInstance().m_graphics.PreferredBackBufferHeight = int.Parse(l_setting[1]); } else if (l_setting[0].Equals("Fullscreen")) { Game.getInstance().m_graphics.IsFullScreen = bool.Parse(l_setting[1]); } else if (l_setting[0].StartsWith("[")) { break; } else { System.Console.WriteLine("Unknown Setting"); } break; } } Game.getInstance().m_graphics.ApplyChanges(); }
protected static void Cmd1F(ROM rom, ParseState state) { state.areaId = rom.Read8(2); }
private void DoStep(string line, ref ParseState state) { if (state == ParseState.Start) { // We're looking for a opening block: // We're specifically looking for "<identifier> { [# anything]" var match = blockstart.Match(line); if (match != null && match.Success) { // We're starting a block _blockName = match.Groups[1].Value; state = ParseState.Block; return; } else { // Could not find the string? wha? if (string.IsNullOrWhiteSpace(LastError)) { LastError = string.Empty; } LastError += "Could not find the start of the next block?" + Environment.NewLine; return; } } else if (state == ParseState.Block) { if (blockend.IsMatch(line)) { _blockName = string.Empty; state = ParseState.Start; return; } var simple = simplevariable.Match(line); if (simple.Success) { _variableType = simple.Groups[1].Value; _variableName = simple.Groups[2].Value; _currentValue = simple.Groups[3].Value; AddConfigVariable(_blockName, _variableType, _variableName, _currentValue); _variableType = _variableName = _currentValue = null; return; } var list = listvariable.Match(line); if (list.Success) { _variableType = list.Groups[1].Value; _variableName = list.Groups[2].Value; _listValues = new List <string>(); state = ParseState.List; return; } if (string.IsNullOrWhiteSpace(LastError)) { LastError = string.Empty; } LastError += $"Unknown token, looking for the end of a block, or a simple variable, or a list variable? {line}" + Environment.NewLine; } else if (state == ParseState.List) { if (listend.IsMatch(line)) { AddConfigVariableList(_blockName, _variableType, _variableName, _listValues); _variableType = _variableName = _currentValue = null; _listValues = null; state = ParseState.Block; return; } var item = listitem.Match(line); if (item.Success) { _listValues.Add(item.Groups[1].Value); return; } if (string.IsNullOrWhiteSpace(LastError)) { LastError = string.Empty; } LastError += $"Unknown token, looking for the end of a list, or a list item? {line}" + Environment.NewLine; } }
// Attempts to add package metadata static private bool TryEvaluatePackage(ref ParseState ioState, StringSlice inLine) { TagData data = TagData.Parse(inLine, ioState.TagDelimiters); if (ioState.Rules.PackageMetaMode == PackageMetaMode.DisallowInBlock) { if (ioState.CurrentState != BlockState.NotStarted && ioState.CurrentState != BlockState.BlockDone) { BlockParser.LogError(ioState.Position, "Cannot set package metadata '{0}', currently in a block", inLine); return(false); } } if (ioState.Rules.PackageMetaMode == PackageMetaMode.ImplicitCloseBlock) { switch (ioState.CurrentState) { case BlockState.NotStarted: case BlockState.BlockDone: break; case BlockState.InHeader: { if (ioState.Rules.RequireExplicitBlockEnd || ioState.Rules.RequireExplicitBlockHeaderEnd) { BlockParser.LogError(ioState.Position, "Cannot close a block with meta command '{0}' before previous block is closed", inLine); return(false); } ioState.Generator.CompleteHeader(ioState, ioState.Package, ioState.CurrentBlock, TagData.Empty); FlushBlock(ref ioState, TagData.Empty); break; } case BlockState.BlockStarted: case BlockState.InData: { if (ioState.Rules.RequireExplicitBlockHeaderEnd) { BlockParser.LogError(ioState.Position, "Cannot close a block with meta command '{0}' before previous block is closed", inLine); return(false); } if (ioState.CurrentState == BlockState.BlockStarted) { ioState.Generator.CompleteHeader(ioState, ioState.Package, ioState.CurrentBlock, TagData.Empty); } FlushBlock(ref ioState, TagData.Empty); break; } } } if (!data.IsEmpty()) { bool bHandled = ioState.Generator.TryEvaluatePackage(ioState, ioState.Package, ioState.CurrentBlock, data); if (!bHandled) { bHandled = ioState.Cache.TryEvaluateCommand(ioState.Package, data); } if (!bHandled) { BlockParser.LogError(ioState.Position, "Unrecognized package metadata '{0}'", data); } return(bHandled); } return(false); }
void _Create() { ptokens tks = new ptokens(erh); m_outname = "syntax"; tks.m_sgen = this; m_lexer = tks; m_symbols.erh = erh; m_symbols.ClassInit(this); m_outname = "syntax"; m_outFile.WriteLine("using System;using Tools;"); Production special = new Production(this, m_symbols.Special); m_lexer.yytext = "error"; CSymbol e = (new CSymbol(this)).Resolve(); e.m_symtype = CSymbol.SymType.nonterminal; e.m_defined = true; // 1: INPUT // top-down parsing of script m_lexer.Start(m_inFile); m_tok = (TOKEN)m_lexer.Next(); //Console.WriteLine("Token <{0}> {1}",m_tok.yytext,m_tok.GetType().Name); while (m_tok != null) { ParseProduction(); } // that's the end of the script if (!m_parserseen) { Error(30, 0, "no parser directive detected - possibly incorrect text encoding?"); } m_outFile.WriteLine(m_actions); // output the action function m_outFile.WriteLine("} return null; }"); special.AddToRhs(m_symbols.m_startSymbol); special.AddToRhs(m_symbols.EOFSymbol); // 2: PROCESSING Console.WriteLine("First"); DoFirst(); Console.WriteLine("Follow"); DoFollow(); Console.WriteLine("Parse Table"); ParseState start = new ParseState(this, null); m_symbols.m_states[0] = start; start.MaybeAdd(new ProdItem(special, 0)); start.Closure(); start.AddEntries(); Transition tfinal = (Transition)start.m_transitions[m_symbols.m_startSymbol.yytext]; ParserShift pe = tfinal.m_next; m_symbols.m_accept = pe.m_next; if (m_symbols.m_accept == null) { m_symbols.erh.Error(new CSToolsFatalException(43, 0, 0, "", "No accept state. ParserGenerator cannot continue.")); } // 2A: Reduce States for the LR(0) parser foreach (ParseState ps in m_symbols.m_states.Values) { ps.ReduceStates(); } /* if (m_showParser) * { * foreach (ParseState ps in m_symbols.m_states.Values) * { * ps.Print0(); * if (ps==m_symbols.m_accept) * Console.WriteLine(" EOF accept"); * } * } */ // 3: Compute Look-ahead sets if (m_lalrParser) { m_symbols.Transitions(new Builder(Transition.BuildDR)); /* if (m_showParser) * m_symbols.PrintTransitions(new Func(Transition.DR),"DR"); */ m_symbols.Transitions(new Builder(Transition.BuildReads)); new Digraph(this, new Relation(Transition.reads), new Func(Transition.DR), new Func(Transition.Read), new AddToFunc(Transition.AddToRead)).Compute(); // detect cycles in Read TBD /* if (m_showParser) * m_symbols.PrintTransitions(new Func(Transition.Read),"Read"); */ m_symbols.Transitions(new Builder(Transition.BuildIncludes)); m_symbols.Transitions(new Builder(Transition.BuildLookback)); new Digraph(this, new Relation(Transition.includes), new Func(Transition.Read), new Func(Transition.Follow), new AddToFunc(Transition.AddToFollow)).Compute(); // detect cycles for which Read is non empty TBD /* if (m_showParser) * m_symbols.PrintTransitions(new Func(Transition.Follow),"Follow"); */ m_symbols.Transitions(new Builder(Transition.BuildLA)); } // 5: OUTPUT // output the run-time ParsingInfo table Console.WriteLine("Building parse table"); m_symbols.Transitions(new Builder(Transition.BuildParseTable)); foreach (CSymbol v in m_symbols.symbols.Values) { if (v.m_symtype != CSymbol.SymType.nodesymbol) { continue; } ParsingInfo pi = new ParsingInfo(v.yytext); CSymbol r = v; while (r.m_symtype == CSymbol.SymType.nodesymbol) { r = r.m_refSymbol; } if (m_symbols.symbolInfo[v.yytext] != null) { m_symbols.erh.Error(new CSToolsException(45, "Bad %node/%symbol hierarchy")); } pi.m_parsetable = m_symbols.GetSymbolInfo(r.yytext).m_parsetable; m_symbols.symbolInfo[v.yytext] = pi; } Console.WriteLine("Writing the output file"); m_outFile.WriteLine(@"/// <summary/>"); m_outFile.WriteLine("public yy" + m_outname + "():base() { arr = new int[] { "); m_symbols.Emit(m_outFile); // output the class factories CSymbol s; Console.WriteLine("Class factories"); IDictionaryEnumerator de = m_symbols.symbols.GetEnumerator(); for (int pos = 0; pos < m_symbols.symbols.Count; pos++) { de.MoveNext(); string str = (string)de.Key; s = (CSymbol)de.Value; if ((s == null) || // might happen because of error recovery (s.m_symtype != CSymbol.SymType.nonterminal && s.m_symtype != CSymbol.SymType.nodesymbol)) { continue; } //Console.WriteLine("{0} {1}",s.yytext,s.m_symtype); m_outFile.WriteLine("new Sfactory(this,\"{0}\",new SCreator({0}_factory));", str); } m_outFile.WriteLine("}"); de.Reset(); for (int pos = 0; pos < m_symbols.symbols.Count; pos++) { de.MoveNext(); string str = (string)de.Key; s = (CSymbol)de.Value; if ((s == null) || // might happen because of error recovery (s.m_symtype != CSymbol.SymType.nonterminal && s.m_symtype != CSymbol.SymType.nodesymbol)) { continue; } //Console.WriteLine("{0} {1}",s.yytext,s.m_symtype); m_outFile.WriteLine(@"/// <summary/>"); m_outFile.WriteLine("public static object " + str + "_factory(Parser yyp) { return new " + str + "(yyp); }"); } m_outFile.WriteLine("}"); m_outFile.WriteLine(@"/// <summary/>"); m_outFile.WriteLine("public class " + m_outname + ": Parser {"); m_outFile.WriteLine(@"/// <summary/>"); m_outFile.WriteLine("public " + m_outname + "():base(new yy" + m_outname + "(),new " + m_lexerClass + "()) {}"); m_outFile.WriteLine(@"/// <summary/>"); m_outFile.WriteLine("public " + m_outname + "(Symbols syms):base(syms,new " + m_lexerClass + "()) {}"); m_outFile.WriteLine(@"/// <summary/>"); m_outFile.WriteLine("public " + m_outname + "(Symbols syms,ErrorHandler erh):base(syms,new " + m_lexerClass + "(erh)) {}"); m_outFile.WriteLine(m_actvars); m_outFile.WriteLine(" }"); if (m_namespace) { m_outFile.WriteLine("}"); } Console.WriteLine("Done"); if (m_showParser) { foreach (ParseState ps in m_symbols.m_states.Values) { ps.Print(); if (ps == m_symbols.m_accept) { Console.WriteLine(" EOF accept"); } } } }
private string drawcard(ParseState state) { var request = state.parameter; if (request != "") { if (state.user.IsBroadcaster || state.user.IsModerator) // BUG: These commands take 2 tiers of permission, perhaps we can handle this better with subcommands. { request = GetBeatSaverId(request); if (request == "") { return(empty); } listcollection.add(state.botcmd.userParameter + ".deck", request); deck[state.botcmd.userParameter.ToString()] += "," + request; QueueChatMessage($"Added {request} to deck {state.botcmd.userParameter.ToString()}."); return(empty); } else { QueueChatMessage("Adding cards to a deck is restricted to moderators."); return(empty); } } if (RequestBotConfig.Instance.RequestQueueOpen == false && !state.flags.HasFlag(CmdFlags.NoFilter) && !state.flags.HasFlag(CmdFlags.Local)) { QueueChatMessage("Queue is currently closed."); return(empty); } while (true) { string[] integerStrings = deck[state.botcmd.userParameter.ToString()].Split(new char[] { ',', ' ', '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); if (integerStrings.Length > 0) { int entry = generator.Next(0, integerStrings.Length); string newlist = ""; for (int x = 0; x < entry; x++) { newlist += integerStrings[x] + ","; } for (int y = entry + 1; y < integerStrings.Length; y++) { newlist += integerStrings[y] + ","; } deck[state.botcmd.userParameter.ToString()] = newlist; string songid = GetBeatSaverId(integerStrings[entry]); if (listcollection.contains(ref duplicatelist, songid)) { continue; } if (listcollection.contains(ref banlist, integerStrings[entry])) { continue; } if (IsInQueue(songid)) { continue; } ParseState newstate = new ParseState(state); // Must use copies here, since these are all threads newstate.flags = CmdFlags.NoFilter; if (state.flags.HasFlag(CmdFlags.SilentResult) || state.flags.HasFlag(CmdFlags.Local)) { newstate.flags |= CmdFlags.SilentResult; } newstate.parameter = integerStrings[entry]; newstate.info = $"Deck: {state.botcmd.userParameter}"; ProcessSongRequest(newstate); } else { QueueChatMessage("Deck is empty."); } break; } return(empty); }
public static Node Parse(ParseState parsestate) { return(parsestate.BaseNode); }
protected sealed override bool HandleWritable(byte[] source) { if (Malformed) { return(false); } char c = '\0'; ulong length, i = 0, sourceLength = (ulong)source.LongLength; for (; i < sourceLength;) { switch (State) { case ParseState.Dormant: return(false); case ParseState.Binary: ContentLength = EncodedContentLength += sourceLength; if (!WriteChunk(source)) { return(false); } if (Type.Value.Length.HasValue && Type.Value.Length == ContentLength) { return(Finish()); } return(true); case ParseState.Chunked_Length: EncodedContentLength++; c = (char)source[i++]; if (c == CR) { State = ParseState.Chunked_LengthLf; } else { ChunkLengthString = ChunkLengthString == null?c.ToString() : ChunkLengthString + c; } break; case ParseState.Chunked_LengthLf: EncodedContentLength++; c = (char)source[i++]; if (c != LF) { return(!(Malformed = true)); } ChunkIndex = 0; if (!ulong.TryParse(ChunkLengthString, NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out length)) { return(!(Malformed = true)); } ChunkLengthString = null; State = (ChunkLength = length) == 0 ? ParseState.Chunked_Trailer : ParseState.Chunked_ChunkData; break; case ParseState.Chunked_ChunkData: length = Math.Min(ChunkLength - ChunkIndex, sourceLength - i); WriteChunk(PrimitiveBuffer.Slice(source, i, i += length)); ChunkIndex += length; ContentLength += length; EncodedContentLength += length; if (ChunkIndex >= ChunkLength) { State = ParseState.Chunked_ChunkCr; } break; case ParseState.Chunked_ChunkCr: EncodedContentLength++; c = (char)source[i++]; if (c != CR) { return(!(Malformed = true)); } State = ParseState.Chunked_ChunkLf; break; case ParseState.Chunked_ChunkLf: EncodedContentLength++; c = (char)source[i++]; if (c != LF) { return(!(Malformed = true)); } ChunkIndex = 0; State = ParseState.Chunked_Length; break; case ParseState.Chunked_Trailer: while ((c = (char)source[i++]) != CR) { EncodedContentLength++; } State = ParseState.Chunked_Lf; break; case ParseState.Chunked_Lf: EncodedContentLength++; c = (char)source[i++]; if (c != LF) { return(!(Malformed = true)); } ExcessStore.Write(source, i); return(Finish()); } } return(true); }
private JsonContainer ParseBinary(BinaryReader jsonBinary, ParseState parseMode = ParseState.Object) { try { Queue <object> tokenStack = new Queue <object> (); bool isParsing = true; int dataSize = jsonBinary.ReadInt32(); int currentPosition = ( int )jsonBinary.BaseStream.Position; while (isParsing && (jsonBinary.BaseStream.Position - currentPosition) != dataSize) { BSONType rb = ( BSONType )jsonBinary.ReadByte(); if (rb == BSONType.EndDoc) { break; } if (parseMode == ParseState.Object) { tokenStack.Enqueue(GetKeyFromBinary(jsonBinary)); } else { jsonBinary.ReadByte(); } switch (rb) { case BSONType.EndDoc: isParsing = false; break; case BSONType.Double: tokenStack.Enqueue(jsonBinary.ReadDouble()); break; case BSONType.String: tokenStack.Enqueue(GetStringFromBinary(jsonBinary)); break; case BSONType.Document: tokenStack.Enqueue(ParseBinary(jsonBinary, ParseState.Object)); break; case BSONType.Array: tokenStack.Enqueue(ParseBinary(jsonBinary, ParseState.Array)); break; case BSONType.BinaryData: tokenStack.Enqueue(GetBinaryFromBinary(jsonBinary)); break; case BSONType.Boolean: tokenStack.Enqueue(jsonBinary.ReadByte() == 0 ? false : true); break; case BSONType.UTCTime: tokenStack.Enqueue(DateTime.FromFileTimeUtc(jsonBinary.ReadInt64())); break; case BSONType.Null: tokenStack.Enqueue(null); break; case BSONType.Regexp: tokenStack.Enqueue(new Regex(GetStringFromBinary(jsonBinary))); break; case BSONType.JavascriptCode: tokenStack.Enqueue(GetStringFromBinary(jsonBinary)); break; case BSONType.Integer: tokenStack.Enqueue(jsonBinary.ReadInt32()); break; case BSONType.Integer64: tokenStack.Enqueue(jsonBinary.ReadInt64()); break; default: throw new Exception("There is unsupport Data type."); } } return(Build(parseMode, tokenStack)); } catch { throw new ArgumentException("Invalid JSON document."); } }
private JsonContainer ParseString(MyStringReader jsonString) { try { ParseState parseMode = ParseState.None; ParseState parseState = ParseState.None; Queue <object> tokenStack = new Queue <object> (); while (true) { char rc = '\0'; do { rc = jsonString.ReadChar(); } while (rc == ' ' || rc == ' '|| rc == ' ' || rc == '\n' || rc == '\r'); if (rc == ':') { if (!(parseMode == ParseState.Object && parseState == ParseState.Key)) { throw new Exception("Invalid JSON document."); } parseState = ParseState.Value; } else if (rc == ',') { if (parseState != ParseState.Value) { throw new Exception("Invalid JSON document."); } parseState = (parseMode == ParseState.Object) ? ParseState.Key : ParseState.Value; } else if (rc == '"') { tokenStack.Enqueue(GetStringFromString(jsonString)); } else if (rc == 't' || rc == 'f' || rc == 'n') { tokenStack.Enqueue(GetKeywordFromString(jsonString, rc)); } else if ((rc >= '0' && rc <= '9') || rc == '-' || rc == '+') { tokenStack.Enqueue(GetNumberFromString(jsonString, rc)); } else if (rc == '{' || rc == '[') { if (parseState == ParseState.Value) { jsonString.Position -= 1; tokenStack.Enqueue(ParseString(jsonString)); } else { switch (rc) { case '{': parseMode = ParseState.Object; parseState = ParseState.Key; break; case '[': parseMode = ParseState.Array; parseState = ParseState.Value; break; } } } else if ((rc == '}' && parseMode == ParseState.Object) || (rc == ']' && parseMode == ParseState.Array)) { break; } else { throw new ArgumentException("Invalid JSON document."); } } return(Build(parseMode, tokenStack)); } catch { throw new ArgumentException("Invalid JSON document."); } }
/// <summary> /// Replace known entities by characters. /// </summary> /// <param name="text">The source text.</param> /// <returns>The result text.</returns> public static string DeEntitize(string text) { if (text == null) { return(null); } if (text.Length == 0) { return(text); } StringBuilder sb = new StringBuilder(text.Length); ParseState state = ParseState.Text; StringBuilder entity = new StringBuilder(10); for (int i = 0; i < text.Length; i++) { switch (state) { case ParseState.Text: switch (text[i]) { case '&': state = ParseState.EntityStart; break; default: sb.Append(text[i]); break; } break; case ParseState.EntityStart: switch (text[i]) { case ';': if (entity.Length == 0) { sb.Append("&;"); } else { if (entity[0] == '#') { string e = entity.ToString(); try { string codeStr = e.Substring(1).Trim().ToLower(); int fromBase; if (codeStr.StartsWith("x")) { fromBase = 16; codeStr = codeStr.Substring(1); } else { fromBase = 10; } int code = Convert.ToInt32(codeStr, fromBase); sb.Append(Convert.ToChar(code)); } catch { sb.Append("&#" + e + ";"); } } else { // named entity? int code; if (!_entityValue.TryGetValue(entity.ToString(), out code)) { // nope sb.Append("&" + entity + ";"); } else { // we found one sb.Append(Convert.ToChar(code)); } } entity.Remove(0, entity.Length); } state = ParseState.Text; break; case '&': // new entity start without end, it was not an entity... sb.Append("&" + entity); entity.Remove(0, entity.Length); break; default: entity.Append(text[i]); if (entity.Length > _maxEntitySize) { // unknown stuff, just don't touch it state = ParseState.Text; sb.Append("&" + entity); entity.Remove(0, entity.Length); } break; } break; } } // finish the work if (state == ParseState.EntityStart) { sb.Append("&" + entity); } return(sb.ToString()); }
/////////////////////////////////////////////////////////////////////// #region IExecute Members public override ReturnCode Execute( Interpreter interpreter, IClientData clientData, ArgumentList arguments, ref Result result ) { ReturnCode code = ReturnCode.Ok; if (interpreter != null) { if (arguments != null) { if (arguments.Count >= 2) { string subCommand = arguments[1]; bool tried = false; code = ScriptOps.TryExecuteSubCommandFromEnsemble( interpreter, this, clientData, arguments, true, false, ref subCommand, ref tried, ref result); if ((code == ReturnCode.Ok) && !tried) { if (code == ReturnCode.Ok) { switch (subCommand) { case "command": { if (arguments.Count >= 3) { OptionDictionary options = new OptionDictionary( new IOption[] { new Option(typeof(EngineFlags), OptionFlags.MustHaveEnumValue | OptionFlags.Unsafe, Index.Invalid, Index.Invalid, "-engineflags", new Variant(interpreter.EngineFlags)), new Option(typeof(SubstitutionFlags), OptionFlags.MustHaveEnumValue, Index.Invalid, Index.Invalid, "-substitutionflags", new Variant(interpreter.SubstitutionFlags)), new Option(null, OptionFlags.MustHaveIntegerValue, Index.Invalid, Index.Invalid, "-startindex", null), new Option(null, OptionFlags.MustHaveIntegerValue, Index.Invalid, Index.Invalid, "-characters", null), new Option(null, OptionFlags.MustHaveBooleanValue, Index.Invalid, Index.Invalid, "-nested", null), new Option(null, OptionFlags.MustHaveBooleanValue, Index.Invalid, Index.Invalid, "-noready", null), new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, Option.EndOfOptions, null) }); int argumentIndex = Index.Invalid; code = interpreter.GetOptions( options, arguments, 0, 2, Index.Invalid, true, ref argumentIndex, ref result); if (code == ReturnCode.Ok) { if ((argumentIndex != Index.Invalid) && ((argumentIndex + 1) == arguments.Count)) { Variant value = null; EngineFlags engineFlags = interpreter.EngineFlags; if (options.IsPresent("-engineflags", ref value)) { engineFlags = (EngineFlags)value.Value; } SubstitutionFlags substitutionFlags = interpreter.SubstitutionFlags; if (options.IsPresent("-substitutionflags", ref value)) { substitutionFlags = (SubstitutionFlags)value.Value; } int startIndex = 0; if (options.IsPresent("-startindex", ref value)) { startIndex = (int)value.Value; } int characters = arguments[argumentIndex].Length; if (options.IsPresent("-characters", ref value)) { characters = (int)value.Value; } bool nested = false; if (options.IsPresent("-nested", ref value)) { nested = (bool)value.Value; } bool noReady = false; if (options.IsPresent("-noready", ref value)) { noReady = (bool)value.Value; } IParseState state = new ParseState( engineFlags, substitutionFlags); code = Parser.ParseCommand( interpreter, arguments[argumentIndex], startIndex, characters, nested, state, noReady, ref result); if (code == ReturnCode.Ok) { // // NOTE: Success, return the entire // state as a string. // result = state.ToString(); } } else { if ((argumentIndex != Index.Invalid) && Option.LooksLikeOption(arguments[argumentIndex])) { result = OptionDictionary.BadOption(options, arguments[argumentIndex]); } else { result = "wrong # args: should be \"parse command ?options? text\""; } code = ReturnCode.Error; } } } else { result = "wrong # args: should be \"parse command ?options? text\""; code = ReturnCode.Error; } break; } case "expression": { if (arguments.Count >= 3) { OptionDictionary options = new OptionDictionary( new IOption[] { new Option(typeof(EngineFlags), OptionFlags.MustHaveEnumValue | OptionFlags.Unsafe, Index.Invalid, Index.Invalid, "-engineflags", new Variant(interpreter.EngineFlags)), new Option(typeof(SubstitutionFlags), OptionFlags.MustHaveEnumValue, Index.Invalid, Index.Invalid, "-substitutionflags", new Variant(interpreter.SubstitutionFlags)), new Option(null, OptionFlags.MustHaveIntegerValue, Index.Invalid, Index.Invalid, "-startindex", null), new Option(null, OptionFlags.MustHaveIntegerValue, Index.Invalid, Index.Invalid, "-characters", null), new Option(null, OptionFlags.MustHaveBooleanValue, Index.Invalid, Index.Invalid, "-noready", null), new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, Option.EndOfOptions, null) }); int argumentIndex = Index.Invalid; code = interpreter.GetOptions( options, arguments, 0, 2, Index.Invalid, true, ref argumentIndex, ref result); if (code == ReturnCode.Ok) { if ((argumentIndex != Index.Invalid) && ((argumentIndex + 1) == arguments.Count)) { Variant value = null; EngineFlags engineFlags = interpreter.EngineFlags; if (options.IsPresent("-engineflags", ref value)) { engineFlags = (EngineFlags)value.Value; } SubstitutionFlags substitutionFlags = interpreter.SubstitutionFlags; if (options.IsPresent("-substitutionflags", ref value)) { substitutionFlags = (SubstitutionFlags)value.Value; } int startIndex = 0; if (options.IsPresent("-startindex", ref value)) { startIndex = (int)value.Value; } int characters = arguments[argumentIndex].Length; if (options.IsPresent("-characters", ref value)) { characters = (int)value.Value; } bool noReady = false; if (options.IsPresent("-noready", ref value)) { noReady = (bool)value.Value; } IParseState state = new ParseState( engineFlags, substitutionFlags); code = ExpressionParser.ParseExpression( interpreter, arguments[argumentIndex], startIndex, characters, state, noReady, ref result); if (code == ReturnCode.Ok) { // // NOTE: Success, return the entire // state as a string. // result = state.ToString(); } } else { if ((argumentIndex != Index.Invalid) && Option.LooksLikeOption(arguments[argumentIndex])) { result = OptionDictionary.BadOption(options, arguments[argumentIndex]); } else { result = "wrong # args: should be \"parse expression ?options? text\""; } code = ReturnCode.Error; } } } else { result = "wrong # args: should be \"parse expression ?options? text\""; code = ReturnCode.Error; } break; } case "options": { if (arguments.Count >= 4) { OptionDictionary options = new OptionDictionary( new IOption[] { new Option(typeof(OptionBehaviorFlags), OptionFlags.MustHaveEnumValue, Index.Invalid, Index.Invalid, "-flags", new Variant(OptionBehaviorFlags.Default)), new Option(null, OptionFlags.MustHaveValue, Index.Invalid, Index.Invalid, "-optionsvar", null), new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-indexes", null), new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-allowinteger", null), new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-strict", null), new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-verbose", null), new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-nocase", null), new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-novalue", null), new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-noset", null), new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-noready", null), new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-simple", null), new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, Option.EndOfOptions, null) }); int argumentIndex = Index.Invalid; code = interpreter.GetOptions( options, arguments, 0, 2, Index.Invalid, true, ref argumentIndex, ref result); if (code == ReturnCode.Ok) { if ((argumentIndex != Index.Invalid) && ((argumentIndex + 2) == arguments.Count)) { Variant value = null; string optionsVarName = Vars.OptionSet.Options; if (options.IsPresent("-optionsvar", ref value)) { optionsVarName = value.ToString(); } OptionBehaviorFlags flags = OptionBehaviorFlags.Default; if (options.IsPresent("-flags", ref value)) { flags = (OptionBehaviorFlags)value.Value; } bool indexes = false; if (options.IsPresent("-indexes")) { indexes = true; } bool allowInteger = false; if (options.IsPresent("-allowinteger")) { allowInteger = true; } bool strict = false; if (options.IsPresent("-strict")) { strict = true; } bool verbose = false; if (options.IsPresent("-verbose")) { verbose = true; } bool noCase = false; if (options.IsPresent("-nocase")) { noCase = true; } bool noValue = false; if (options.IsPresent("-novalue")) { noValue = true; } bool noSet = false; if (options.IsPresent("-noset")) { noSet = true; } bool noReady = false; if (options.IsPresent("-noready")) { noReady = true; } bool simple = false; if (options.IsPresent("-simple")) { simple = true; } OptionDictionary newOptions = null; AppDomain appDomain = interpreter.GetAppDomain(); CultureInfo cultureInfo = interpreter.CultureInfo; if (simple) { newOptions = OptionDictionary.FromString( interpreter, arguments[argumentIndex], appDomain, Value.GetTypeValueFlags( allowInteger, strict, verbose, noCase), cultureInfo, ref result); } else { newOptions = OptionDictionary.FromString( interpreter, arguments[argumentIndex], appDomain, allowInteger, strict, verbose, noCase, cultureInfo, ref result); } if (newOptions != null) { StringList list = StringList.FromString( arguments[argumentIndex + 1], ref result); if (list != null) { ArgumentList newArguments = new ArgumentList(list); int nextIndex = Index.Invalid; int endIndex = Index.Invalid; code = interpreter.GetOptions( newOptions, newArguments, 0, 0, Index.Invalid, flags, noCase, noValue, noSet, ref nextIndex, ref endIndex, ref result); if (code == ReturnCode.Ok) { VariableFlags variableFlags = VariableFlags.None; if (noReady) { variableFlags |= VariableFlags.NoReady; } if (indexes) { code = interpreter.SetVariableValue2( variableFlags, optionsVarName, Vars.OptionSet.NextIndex, nextIndex.ToString(), null, ref result); if (code == ReturnCode.Ok) { code = interpreter.SetVariableValue2( variableFlags, optionsVarName, Vars.OptionSet.EndIndex, endIndex.ToString(), null, ref result); } } if (code == ReturnCode.Ok) { foreach (KeyValuePair <string, IOption> pair in newOptions) { IOption option = pair.Value; if (option == null) { continue; } if (option.IsIgnored(newOptions)) { continue; } /* REUSED */ value = null; bool present = option.IsPresent(newOptions, ref value); if (present && !option.CanBePresent(newOptions, ref result)) { code = ReturnCode.Error; break; } code = interpreter.SetVariableValue2( variableFlags, optionsVarName, pair.Key, present.ToString(), null, ref result); if (code != ReturnCode.Ok) { break; } if (option.MustHaveValue(newOptions)) { // // NOTE: If the option was not actually present, // grab and use the default value instead. // if (!present) { value = option.Value; } // // NOTE: Only set the value if the option was // actually present OR there is a bonafide // default value. // if (present || (value != null)) { string index = pair.Key + Characters.Comma + Vars.OptionSet.Value; code = interpreter.SetVariableValue2( variableFlags, optionsVarName, index, (value != null) ? value.ToString() : null, null, ref result); if (code != ReturnCode.Ok) { break; } } } } if (code == ReturnCode.Ok) { result = String.Empty; } } } } else { code = ReturnCode.Error; } } else { code = ReturnCode.Error; } } else { if ((argumentIndex != Index.Invalid) && Option.LooksLikeOption(arguments[argumentIndex])) { result = OptionDictionary.BadOption(options, arguments[argumentIndex]); } else { result = "wrong # args: should be \"parse options ?options? optionList argumentList\""; } code = ReturnCode.Error; } } } else { result = "wrong # args: should be \"parse options ?options? options optionList argumentList\""; code = ReturnCode.Error; } break; } case "script": { if (arguments.Count >= 3) { OptionDictionary options = new OptionDictionary( new IOption[] { new Option(typeof(EngineFlags), OptionFlags.MustHaveEnumValue | OptionFlags.Unsafe, Index.Invalid, Index.Invalid, "-engineflags", new Variant(interpreter.EngineFlags)), new Option(typeof(SubstitutionFlags), OptionFlags.MustHaveEnumValue, Index.Invalid, Index.Invalid, "-substitutionflags", new Variant(interpreter.SubstitutionFlags)), new Option(null, OptionFlags.MustHaveValue, Index.Invalid, Index.Invalid, "-filename", null), new Option(null, OptionFlags.MustHaveIntegerValue, Index.Invalid, Index.Invalid, "-currentline", null), new Option(null, OptionFlags.MustHaveIntegerValue, Index.Invalid, Index.Invalid, "-startindex", null), new Option(null, OptionFlags.MustHaveIntegerValue, Index.Invalid, Index.Invalid, "-characters", null), new Option(null, OptionFlags.MustHaveBooleanValue, Index.Invalid, Index.Invalid, "-nested", null), new Option(null, OptionFlags.MustHaveBooleanValue, Index.Invalid, Index.Invalid, "-syntax", null), new Option(null, OptionFlags.MustHaveBooleanValue, Index.Invalid, Index.Invalid, "-strict", null), new Option(null, OptionFlags.MustHaveBooleanValue, Index.Invalid, Index.Invalid, "-roundtrip", null), new Option(null, OptionFlags.MustHaveBooleanValue, Index.Invalid, Index.Invalid, "-noready", null), new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, Option.EndOfOptions, null) }); int argumentIndex = Index.Invalid; code = interpreter.GetOptions( options, arguments, 0, 2, Index.Invalid, true, ref argumentIndex, ref result); if (code == ReturnCode.Ok) { if ((argumentIndex != Index.Invalid) && ((argumentIndex + 1) == arguments.Count)) { Variant value = null; EngineFlags engineFlags = interpreter.EngineFlags; if (options.IsPresent("-engineflags", ref value)) { engineFlags = (EngineFlags)value.Value; } SubstitutionFlags substitutionFlags = interpreter.SubstitutionFlags; if (options.IsPresent("-substitutionflags", ref value)) { substitutionFlags = (SubstitutionFlags)value.Value; } string fileName = null; if (options.IsPresent("-filename", ref value)) { fileName = value.ToString(); } int currentLine = Parser.StartLine; if (options.IsPresent("-currentline", ref value)) { currentLine = (int)value.Value; } int startIndex = 0; if (options.IsPresent("-startindex", ref value)) { startIndex = (int)value.Value; } int characters = arguments[argumentIndex].Length; if (options.IsPresent("-characters", ref value)) { characters = (int)value.Value; } bool nested = false; if (options.IsPresent("-nested", ref value)) { nested = (bool)value.Value; } bool syntax = false; if (options.IsPresent("-syntax", ref value)) { syntax = (bool)value.Value; } bool strict = false; if (options.IsPresent("-strict", ref value)) { strict = (bool)value.Value; } bool roundTrip = false; if (options.IsPresent("-roundtrip", ref value)) { roundTrip = (bool)value.Value; } bool noReady = false; if (options.IsPresent("-noready", ref value)) { noReady = (bool)value.Value; } IParseState state = new ParseState( engineFlags, substitutionFlags); TokenList tokens = null; code = Parser.ParseScript( interpreter, fileName, currentLine, arguments[argumentIndex], startIndex, characters, engineFlags, substitutionFlags, nested, noReady, syntax, strict, ref state, ref tokens, ref result); if (code == ReturnCode.Ok) { if (roundTrip) { // // NOTE: Return only the tokens that // are absolutely necessary to // rebuild the script text. // TokenList newTokens = new TokenList(); for (int index = 0; index < tokens.Count; index++) { IToken token = tokens[index]; if (token.Type == TokenType.Variable) { index += token.Components; } else if ((token.Type != TokenType.Separator) && (token.Components != 0)) { continue; } newTokens.Add(token); } result = newTokens.ToString(); } else { // // NOTE: Replace final token list // with the one we have been // building. // state.Tokens = tokens; // // NOTE: Success, return the entire // state as a string. // result = state.ToString(); } } } else { if ((argumentIndex != Index.Invalid) && Option.LooksLikeOption(arguments[argumentIndex])) { result = OptionDictionary.BadOption(options, arguments[argumentIndex]); } else { result = "wrong # args: should be \"parse script ?options? text\""; } code = ReturnCode.Error; } } } else { result = "wrong # args: should be \"parse script ?options? text\""; code = ReturnCode.Error; } break; } default: { result = ScriptOps.BadSubCommand( interpreter, null, null, subCommand, this, null, null); code = ReturnCode.Error; break; } } } } } else { result = "wrong # args: should be \"parse type ?arg ...?\""; code = ReturnCode.Error; } } else { result = "invalid argument list"; code = ReturnCode.Error; } } else { result = "invalid interpreter"; code = ReturnCode.Error; } return(code); }
public Instruction ParseSingle(ParseState ParseState) { var InstructionPosition = Stream.Position; var OpcodeNum = BinaryReader.ReadUInt16(); if (!Opcodes.ContainsKey(OpcodeNum)) { throw(new NotImplementedException(String.Format("Unhandled opcode 0x{0:X2}", OpcodeNum))); } var Opcode = Opcodes[OpcodeNum]; var Params = new List <object>(); foreach (var FormatChar in Opcode.Format) { switch (FormatChar) { case '<': break; case 's': Params.Add(ReadString()); break; case 'S': Params.Add(ReadStringz()); break; case '1': Params.Add(BinaryReader.ReadByte()); break; case '7': Params.Add((AritmeticOps)BinaryReader.ReadByte()); break; case '9': Params.Add((ComparisionOps)BinaryReader.ReadByte()); break; case '2': Params.Add(BinaryReader.ReadInt16()); break; case 'v': Params.Add(new VariableReference() { Index = BinaryReader.ReadUInt16() }); break; case '4': Params.Add(BinaryReader.ReadUInt32()); break; case 'L': Params.Add(new LabelReference() { Offset = BinaryReader.ReadUInt32() }); break; case 'P': { var ParamType = BinaryReader.ReadByte(); object Value = null; switch (ParamType) { case 0x00: Value = BinaryReader.ReadSByte(); break; case 0x10: Value = BinaryReader.ReadByte(); break; case 0x20: Value = BinaryReader.ReadInt16(); break; case 0x40: Value = BinaryReader.ReadInt32(); break; case 0x01: Value = new VariableReference() { Index = BinaryReader.ReadUInt16() }; break; case 0x02: Value = new SpecialReference() { Index = BinaryReader.ReadUInt16() }; break; default: throw (new NotImplementedException(String.Format("Invalid param type {0}", ParamType))); } Params.Add(Value); } break; default: throw(new NotImplementedException(String.Format("Invalid format '{0}'", FormatChar))); } } try { if (ParseState.Unindent.Contains((uint)InstructionPosition)) { ParseState.Indent--; } switch (Opcode.OpcodeName) { case "JUMP_ALWAYS": ParseState.Indent--; break; } return(new Instruction() { Position = (uint)InstructionPosition, Opcode = Opcode, Parameters = Params.ToArray(), Indent = ParseState.Indent, }); } finally { switch (Opcode.OpcodeName) { //case "FUNCTION_DEF": case "JUMP_IF": ParseState.Unindent.Add(((LabelReference)(Params.ToArray()[3])).Offset); ParseState.Indent++; break; case "JUMP_ALWAYS": ParseState.Unindent.Add(((LabelReference)(Params.ToArray()[0])).Offset); ParseState.Indent += 2; break; } } }
// BUG: Not production ready, will probably never be released. Takes hours, and puts a load on beatsaver.com. DO NOT USE public async Task DownloadEverything(ParseState state) { var startingmem = GC.GetTotalMemory(true); Instance.QueueChatMessage("Starting Beatsaver scan"); var StarTime = DateTime.UtcNow; int totalSongs = 0; string requestUrl = "https://beatsaver.com/api/maps/latest"; int offset = 0; while (true) // MaxiumAddScanRange { var resp = await Plugin.WebClient.GetAsync($"{requestUrl}/{offset}", System.Threading.CancellationToken.None); if (resp.IsSuccessStatusCode) { JSONNode result = resp.ConvertToJsonNode(); if (result == null || result["docs"].Count == 0) { break; } foreach (JSONObject entry in result["docs"].AsArray) { JSONObject song = entry; if (MapDatabase.MapLibrary.ContainsKey(song["key"].Value)) { goto done; } new SongMap(song); totalSongs++; if ((totalSongs & 127) == 0) { Instance.QueueChatMessage($"Processed {totalSongs}"); } //QueueSong(state, song); } } else { break; } offset += 1; // Magic beatsaver.com skip constant. } done: var duration = DateTime.UtcNow - StarTime; Instance.QueueChatMessage($"BeatSaver Database Scan done. ({duration.TotalSeconds} secs."); StarTime = DateTime.UtcNow; Instance.QueueChatMessage("Starting Full Download"); var msg = new QueueLongMessage(9999); Instance.QueueChatMessage($"Attempting to download up to {MapDatabase.LevelId.Count} files."); foreach (var song in MapDatabase.LevelId) { if (song.Value.path == "") { string localPath = $"f:\\bsnew\\{song.Value.song["key"].Value}.zip"; if (File.Exists(localPath)) { continue; } var songBytes = await Plugin.WebClient.DownloadSong(song.Value.song["downloadUrl"].Value, System.Threading.CancellationToken.None); File.WriteAllBytes(localPath, songBytes); if (!File.Exists(localPath)) { continue; } msg.Add($"{song.Value.song["id"].Value}", ","); } } msg.end(); duration = DateTime.UtcNow - StarTime; Instance.QueueChatMessage($"BeatSaver Download Done. ({duration.TotalSeconds} secs."); #if UNRELEASED GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce; GC.Collect(); Instance.QueueChatMessage($"hashentries: {SongMap.hashcount} memory: {(GC.GetTotalMemory(false) - startingmem) / 1048576} MB"); #endif }
protected static void Cmd27(ROM rom, ParseState state) { Warp warp = new Warp(0xFF, state.level, rom, rom.offset); state.warps.Add(warp); }
private void ResetState() { _state = default(ParseState); _olderStates.Clear(); }
private IEnumerator GetRooms(ParseState state) { yield break; }
private void DiscardSpeculation() => _state = _olderStates.Pop();
public static Node Parse(ParseState state) { return(state.BuildParenNumberNode(true)); }
private static (string Name, string Value) ParseLine(string line) { var name = new StringBuilder(); var value = new StringBuilder(); ParseState state = ParseState.Key; var whitespace = new StringBuilder(); var escapedspace = new StringBuilder(); bool finished = false; for (int i = 0; i < line.Length; ++i) { char c = line[i]; switch (state) { case ParseState.Key: { switch (c) { case '\\': if (i < line.Length - 1) { c = line[++i]; switch (c) { case '\\': case '#': case '=': name.Append(whitespace); whitespace.Clear(); name.Append(c); break; case ' ': if (name.Length != 0) { whitespace.Append(c); } break; default: name.Append(whitespace); whitespace.Clear(); name.Append('\\'); name.Append(c); break; } } else { name.Append(whitespace); name.Append(c); } break; case ' ': case '\t': case '\r': case '\n': if (name.Length != 0) { whitespace.Append(c); } break; case '=': whitespace.Clear(); state = ParseState.Value; break; case '#': finished = true; break; default: name.Append(whitespace); whitespace.Clear(); name.Append(c); break; } break; } case ParseState.Value: { switch (c) { case '\\': if (i < line.Length - 1) { c = line[++i]; switch (c) { case '\\': case '#': case '=': value.Append(value.Length == 0 ? escapedspace : whitespace); whitespace.Clear(); escapedspace.Clear(); value.Append(c); break; case ' ': whitespace.Append(c); escapedspace.Append(c); break; default: value.Append(value.Length == 0 ? escapedspace : whitespace); whitespace.Clear(); escapedspace.Clear(); value.Append('\\'); value.Append(c); break; } } else { value.Append(value.Length == 0 ? escapedspace : whitespace); value.Append(c); } break; case ' ': case '\t': case '\r': case '\n': if (value.Length != 0) { whitespace.Append(c); } break; case '#': finished = true; break; default: value.Append(value.Length == 0 ? escapedspace : whitespace); whitespace.Clear(); escapedspace.Clear(); value.Append(c); break; } break; } } if (finished) { break; } } value.Append(escapedspace); if ((state == ParseState.Key && name.Length != 0) || (state == ParseState.Value && name.Length == 0)) { throw new FormatException($"invalid config file entry: \"{line}\""); } return(name.ToString(), value.ToString()); }
private List <string> SplitArgs(string commandString) { // We split on whitespace and specially handle quoted strings (quoted strings count as a single arg) // List <string> args = new List <string>(); commandString = commandString.Trim(); StringBuilder sb = new StringBuilder(); ParseState state = ParseState.NonWhiteSpace; foreach (char c in commandString) { switch (state) { case ParseState.NonWhiteSpace: if (char.IsWhiteSpace(c)) { // End of token args.Add(sb.ToString()); sb.Clear(); state = ParseState.WhiteSpace; } else if (c == '\"') { // Start of quoted string state = ParseState.QuotedString; } else { // Character in token sb.Append(c); } break; case ParseState.WhiteSpace: if (!char.IsWhiteSpace(c)) { // Start of new token if (c != '\"') { sb.Append(c); state = ParseState.NonWhiteSpace; } else { // Start of quoted string state = ParseState.QuotedString; } } break; case ParseState.QuotedString: if (c == '\"') { // End of quoted string. args.Add(sb.ToString()); sb.Clear(); state = ParseState.WhiteSpace; } else { // Character in quoted string sb.Append(c); } break; } } if (sb.Length > 0) { // Add the last token to the args list args.Add(sb.ToString()); } return(args); }
public static AppContext Parse(string[] args) { AppContext result = new AppContext(); ParseState state = ParseState.ExpectOption; foreach (var arg in args) { switch (state) { case ParseState.ExpectOption: if (arg.Length > 1 && (arg[0] == '-' || arg[0] == '/')) { switch (Char.ToLowerInvariant(arg[1])) { case '?': result.ShowHelp = true; break; case 'b': state = ParseState.ExpectBaseFolder; break; case 'c': state = ParseState.ExpectConfigFileName; break; case 'p': state = ParseState.ExpectProfileName; break; case 'l': if (arg.Length == 2) { result.ListShortProfiles = true; } else if (arg.Length > 2) { var argOption = arg[2]; if (argOption == '+') { result.ListLongProfiles = true; } } break; case 'r': if (arg.Length == 2) { result.Recurse = true; } else if (arg.Length > 2) { var argOption = arg[2]; if (argOption == '-') { result.Recurse = false; } else if (argOption == '+') { result.Recurse = true; } } break; case 's': state = ParseState.ExpectSourceRegex; break; case 'd': state = ParseState.ExpectDestRegex; break; case 'v': state = ParseState.ExpectVariableTypes; break; case 'w': result.WhatIf = true; break; case 'x': result.ShowExampleSettings = true; break; default: break; } } break; case ParseState.ExpectBaseFolder: result.BaseFolder = arg; state = ParseState.ExpectOption; break; case ParseState.ExpectConfigFileName: result.ConfigFileName = arg; state = ParseState.ExpectOption; break; case ParseState.ExpectProfileName: result.ProfileName = arg; state = ParseState.ExpectOption; break; case ParseState.ExpectSourceRegex: result.SourceRegex = arg; state = ParseState.ExpectOption; break; case ParseState.ExpectDestRegex: result.DestRegex = arg; state = ParseState.ExpectOption; break; case ParseState.ExpectVariableTypes: result.VariableTypes = ConvertStringToNameValueDictionary(arg); state = ParseState.ExpectOption; break; default: break; } } return(result); }
private bool NewCheck() { if (this._c != 60) { return(false); } if ((this._index >= this._text.Length) || (this._text[this._index] != '%')) { if (!this.PushNodeEnd(this._index - 1, true)) { this._index = this._text.Length; return(true); } this._state = ParseState.WhichTag; if (((this._index - 1) <= (this._text.Length - 2)) && (this._text[this._index] == '!')) { this.PushNodeStart(HtmlNodeType.Comment, this._index - 1); this.PushNodeNameStart(true, this._index); this.PushNodeNameEnd(this._index + 1); this._state = ParseState.Comment; if (this._index < (this._text.Length - 2)) { if ((this._text[this._index + 1] == '-') && (this._text[this._index + 2] == '-')) { this._fullcomment = true; } else { this._fullcomment = false; } } return(true); } this.PushNodeStart(HtmlNodeType.Element, this._index - 1); return(true); } ParseState state = this._state; switch (state) { case ParseState.WhichTag: this.PushNodeNameStart(true, this._index - 1); this._state = ParseState.Tag; break; case ParseState.Tag: break; case ParseState.BetweenAttributes: this.PushAttributeNameStart(this._index - 1); break; default: if (state == ParseState.AttributeAfterEquals) { this.PushAttributeValueStart(this._index - 1); } break; } this._oldstate = this._state; this._state = ParseState.ServerSideCode; return(true); }
internal override InternalResult <T> Parse(ref ParseState <TToken> state) { throw new NotImplementedException(); }
private void Parse() { int num = 0; this._lastnodes = new Hashtable(); this._c = 0; this._fullcomment = false; this._parseerrors = new ArrayList(); this._line = 1; this._lineposition = 1; this._maxlineposition = 1; this._state = ParseState.Text; this._oldstate = this._state; this._documentnode._innerlength = this._text.Length; this._documentnode._outerlength = this._text.Length; this._remainderOffset = this._text.Length; this._lastparentnode = this._documentnode; this._currentnode = this.CreateNode(HtmlNodeType.Text, 0); this._currentattribute = null; this._index = 0; this.PushNodeStart(HtmlNodeType.Text, 0); while (this._index < this._text.Length) { this._c = this._text[this._index]; this.IncrementPosition(); switch (this._state) { case ParseState.Text: { if (!this.NewCheck()) { } continue; } case ParseState.WhichTag: if (this.NewCheck()) { continue; } if (this._c != 0x2f) { break; } this.PushNodeNameStart(false, this._index); goto Label_016C; case ParseState.Tag: { if (!this.NewCheck()) { if (!IsWhiteSpace(this._c)) { goto Label_01B6; } this.PushNodeNameEnd(this._index - 1); if (this._state == ParseState.Tag) { this._state = ParseState.BetweenAttributes; } } continue; } case ParseState.BetweenAttributes: { if (!this.NewCheck() && !IsWhiteSpace(this._c)) { if ((this._c != 0x2f) && (this._c != 0x3f)) { goto Label_0292; } this._state = ParseState.EmptyTag; } continue; } case ParseState.EmptyTag: { if (!this.NewCheck()) { if (this._c != 0x3e) { goto Label_035F; } if (this.PushNodeEnd(this._index, true)) { goto Label_033A; } this._index = this._text.Length; } continue; } case ParseState.AttributeName: { if (!this.NewCheck()) { if (!IsWhiteSpace(this._c)) { goto Label_039D; } this.PushAttributeNameEnd(this._index - 1); this._state = ParseState.AttributeBeforeEquals; } continue; } case ParseState.AttributeBeforeEquals: { if (!this.NewCheck() && !IsWhiteSpace(this._c)) { if (this._c != 0x3e) { goto Label_0495; } if (this.PushNodeEnd(this._index, false)) { goto Label_0470; } this._index = this._text.Length; } continue; } case ParseState.AttributeAfterEquals: { if (!this.NewCheck() && !IsWhiteSpace(this._c)) { if ((this._c != 0x27) && (this._c != 0x22)) { goto Label_050C; } this._state = ParseState.QuotedAttributeValue; this.PushAttributeValueStart(this._index); num = this._c; } continue; } case ParseState.AttributeValue: { if (!this.NewCheck()) { if (!IsWhiteSpace(this._c)) { goto Label_05AC; } this.PushAttributeValueEnd(this._index - 1); this._state = ParseState.BetweenAttributes; } continue; } case ParseState.Comment: { if ((this._c == 0x3e) && (!this._fullcomment || ((this._text[this._index - 2] == '-') && (this._text[this._index - 3] == '-')))) { if (!this.PushNodeEnd(this._index, false)) { this._index = this._text.Length; } else { this._state = ParseState.Text; this.PushNodeStart(HtmlNodeType.Text, this._index); } } continue; } case ParseState.QuotedAttributeValue: { if (this._c != num) { goto Label_0634; } this.PushAttributeValueEnd(this._index - 1); this._state = ParseState.BetweenAttributes; continue; } case ParseState.ServerSideCode: { if (((this._c != 0x25) || (this._index >= this._text.Length)) || (this._text[this._index] != '>')) { continue; } ParseState state2 = this._oldstate; if (state2 == ParseState.BetweenAttributes) { goto Label_0765; } if (state2 != ParseState.AttributeAfterEquals) { goto Label_077C; } this._state = ParseState.AttributeValue; goto Label_0788; } case ParseState.PcData: { if (((this._currentnode._namelength + 3) <= (this._text.Length - (this._index - 1))) && (string.Compare(this._text.Substring(this._index - 1, this._currentnode._namelength + 2), "</" + this._currentnode.Name, true) == 0)) { int c = this._text[((this._index - 1) + 2) + this._currentnode.Name.Length]; if ((c == 0x3e) || IsWhiteSpace(c)) { HtmlNode newChild = this.CreateNode(HtmlNodeType.Text, this._currentnode._outerstartindex + this._currentnode._outerlength); newChild._outerlength = (this._index - 1) - newChild._outerstartindex; this._currentnode.AppendChild(newChild); this.PushNodeStart(HtmlNodeType.Element, this._index - 1); this.PushNodeNameStart(false, (this._index - 1) + 2); this._state = ParseState.Tag; this.IncrementPosition(); } } continue; } default: { continue; } } this.PushNodeNameStart(true, this._index - 1); this.DecrementPosition(); Label_016C: this._state = ParseState.Tag; continue; Label_01B6: if (this._c == 0x2f) { this.PushNodeNameEnd(this._index - 1); if (this._state == ParseState.Tag) { this._state = ParseState.EmptyTag; } } else if (this._c == 0x3e) { this.PushNodeNameEnd(this._index - 1); if (this._state == ParseState.Tag) { if (!this.PushNodeEnd(this._index, false)) { this._index = this._text.Length; } else if (this._state == ParseState.Tag) { this._state = ParseState.Text; this.PushNodeStart(HtmlNodeType.Text, this._index); } } } continue; Label_0292: if (this._c == 0x3e) { if (!this.PushNodeEnd(this._index, false)) { this._index = this._text.Length; } else if (this._state == ParseState.BetweenAttributes) { this._state = ParseState.Text; this.PushNodeStart(HtmlNodeType.Text, this._index); } } else { this.PushAttributeNameStart(this._index - 1); this._state = ParseState.AttributeName; } continue; Label_033A: if (this._state == ParseState.EmptyTag) { this._state = ParseState.Text; this.PushNodeStart(HtmlNodeType.Text, this._index); } continue; Label_035F: this._state = ParseState.BetweenAttributes; continue; Label_039D: if (this._c == 0x3d) { this.PushAttributeNameEnd(this._index - 1); this._state = ParseState.AttributeAfterEquals; } else if (this._c == 0x3e) { this.PushAttributeNameEnd(this._index - 1); if (!this.PushNodeEnd(this._index, false)) { this._index = this._text.Length; } else if (this._state == ParseState.AttributeName) { this._state = ParseState.Text; this.PushNodeStart(HtmlNodeType.Text, this._index); } } continue; Label_0470: if (this._state == ParseState.AttributeBeforeEquals) { this._state = ParseState.Text; this.PushNodeStart(HtmlNodeType.Text, this._index); } continue; Label_0495: if (this._c == 0x3d) { this._state = ParseState.AttributeAfterEquals; } else { this._state = ParseState.BetweenAttributes; this.DecrementPosition(); } continue; Label_050C: if (this._c == 0x3e) { if (!this.PushNodeEnd(this._index, false)) { this._index = this._text.Length; } else if (this._state == ParseState.AttributeAfterEquals) { this._state = ParseState.Text; this.PushNodeStart(HtmlNodeType.Text, this._index); } } else { this.PushAttributeValueStart(this._index - 1); this._state = ParseState.AttributeValue; } continue; Label_05AC: if (this._c == 0x3e) { this.PushAttributeValueEnd(this._index - 1); if (!this.PushNodeEnd(this._index, false)) { this._index = this._text.Length; } else if (this._state == ParseState.AttributeValue) { this._state = ParseState.Text; this.PushNodeStart(HtmlNodeType.Text, this._index); } } continue; Label_0634: if (((this._c == 60) && (this._index < this._text.Length)) && (this._text[this._index] == '%')) { this._oldstate = this._state; this._state = ParseState.ServerSideCode; } continue; Label_0765: this.PushAttributeNameEnd(this._index + 1); this._state = ParseState.BetweenAttributes; goto Label_0788; Label_077C: this._state = this._oldstate; Label_0788: this.IncrementPosition(); } if (this._currentnode._namestartindex > 0) { this.PushNodeNameEnd(this._index); } this.PushNodeEnd(this._index, false); this._lastnodes.Clear(); }
public override InternalResult <SourcePos> Parse(ref ParseState <TToken> state) => InternalResult.Success(state.ComputeSourcePos(), false);
// Given an output of 'svn diff' command, return an array of strings, 2 strings for each // file that contains a diff, first string is a file name, second is a revision against which // the local copy diff is made public static List <FileNameAndRev> ParseDiffOutput(string diffTxt) { StringReader reader = new StringReader(diffTxt); var res = new List <FileNameAndRev>(); ParseState state = ParseState.EXPECT_INDEX; string txt; string fileName = null; while (true) { txt = reader.ReadLine(); if (null == txt) { break; } switch (state) { case ParseState.EXPECT_INDEX: if (!txt.StartsWith(indexTxt)) { // shouldn't happen return(res); } fileName = txt.Substring(indexTxt.Length); state = ParseState.AFTER_INDEX; break; case ParseState.AFTER_INDEX: if (!txt.StartsWith(sepTxt)) { // assumption: we might get indexTxt in the body of the diff // so we allow it to not be followed by sepTxt. If it's not // then we'll go into diff skipping mode // this should be very rare, thought! Debug.Assert(false, "Did we really get this in the diff?"); state = ParseState.SKIP_DIFF; break; } state = ParseState.AFTER_SEP; break; case ParseState.AFTER_SEP: if (txt.StartsWith(binaryTxt)) { state = ParseState.SKIP_DIFF; break; } Debug.Assert(txt.StartsWith("---")); Debug.Assert(-1 != txt.IndexOf(fileName)); Match match = Regex.Match(txt, @"\(revision[^\d]+(\d+)\)"); string rev = match.Groups[1].Value; var fi = new FileNameAndRev(); fi.FileName = fileName; fi.Revision = rev; res.Add(fi); state = ParseState.AFTER_REV; break; case ParseState.AFTER_REV: Debug.Assert(txt.StartsWith("+++")); Debug.Assert(-1 != txt.IndexOf(fileName)); state = ParseState.SKIP_DIFF; break; case ParseState.SKIP_DIFF: if (txt.StartsWith(indexTxt)) { state = ParseState.AFTER_INDEX; fileName = txt.Substring(indexTxt.Length); } break; default: Debug.Assert(false, String.Format("Unkown state {0}", state)); break; } } return(res); }
public static Node Parse(ParseState parsestate) { Node basenode = parsestate.BuildParenNumberNode(true); if (basenode == null) { #warning Hack parsestate.BaseNode.Children.Add(Node.ZeroNode); basenode = parsestate.BaseNode; } if (parsestate.CurrentOperator != Operator.Equals) { return(null); } ++parsestate.TokenIndex; Node arg1 = parsestate.BuildNode(false); if (arg1 == null) { return(null); } basenode.Children.Add(arg1); if (parsestate.CurrentSymbol != Symbol.Comma) { #warning Hack basenode.Children.Add(Node.ZeroNode); basenode.Arguments.Add(Operator.Equals); return(basenode); } ++parsestate.TokenIndex; Operator @operator = parsestate.CurrentOperator; if (@operator == Operator.Equals || @operator == Operator.NotEquals) { ++parsestate.TokenIndex; Node rangenode = parsestate.BuildRangeNode(); if (rangenode != null) { basenode.Children.Add(rangenode.Children[1]); basenode.Children.Add(rangenode.Children[2]); basenode.Arguments.Add(@operator); basenode.Arguments.Add(rangenode.Arguments[1]); basenode.Arguments.Add(rangenode.Arguments[2]); return(basenode); } --parsestate.TokenIndex; } switch (@operator) { case Operator.Equals: case Operator.NotEquals: case Operator.GreaterEquals: case Operator.LesserEquals: case Operator.Lesser: case Operator.Greater: ++parsestate.TokenIndex; break; default: return(null); } Node arg = parsestate.BuildNode(false); if (arg == null) { return(null); } basenode.Arguments.Add(@operator); basenode.Children.Add(arg); return(basenode); }
static void parseExpression(string filter, ref int pos, FilterExpressionGroup parentSrchExpGrp) { ParseState state = ParseState.Left; bool hasBracket = false, inString = false, isNot = false; string fieldName = null; object searchVal = null; var sbTemp = new StringBuilder(); ComparisonOperatorEnum comparisonOp = ComparisonOperatorEnum.Equal; MatchTypeEnum matchType = MatchTypeEnum.UseCase; BoolOpEnum curBoolOp = BoolOpEnum.And; int startPos = pos; // skip past any leading spaces while (pos < filter.Length && char.IsWhiteSpace(filter[pos])) { pos++; } for ( ; pos < filter.Length;) { ////////////////////////////////////////////////////////// #region Left if (state == ParseState.Left) { if (filter[pos] == '[') // field names with ' ' in them must be wrapped with brackets { hasBracket = true; pos++; startPos = pos; } if (hasBracket) { // look for ending bracket if (filter[pos] == ']') { fieldName = filter.Substring(startPos, pos - startPos).Trim(); pos++; // skip past bracket } } else // no bracket - look for non-alpha { if (filter[pos] == '(') { // start of a new FilterExpressionGroup pos++; var newSrchExpGrp = new FilterExpressionGroup(); parentSrchExpGrp.Add(curBoolOp, newSrchExpGrp); parseExpression(filter, ref pos, newSrchExpGrp); state = ParseState.BoolOp; } else if (filter[pos] == '~') // eg. ~LastName { matchType = MatchTypeEnum.IgnoreCase; } else if (char.IsWhiteSpace(filter[pos]) || (!char.IsLetterOrDigit(filter[pos]) && filter[pos] != '_' && filter[pos] != '~')) // field names with spaces in them must be wrapped with brackets { fieldName = filter.Substring(startPos, pos - startPos).Trim(); } } if (fieldName != null) { if (fieldName[0] == '~') { fieldName = fieldName.Substring(1); matchType = MatchTypeEnum.IgnoreCase; } state = ParseState.CompareOp; } else { pos++; } } #endregion Left ////////////////////////////////////////////////////////// #region CompareOp else if (state == ParseState.CompareOp) { // skip whitespace while (pos < filter.Length && char.IsWhiteSpace(filter[pos])) { pos++; } if (char.IsLetter(filter[pos])) // REGEX { // should be CONTAINS, REGEX, IN or NOT //if( pos + 4 >= filter.Length ) // throwInvalidFilterConstruct( filter, pos ); try { // NOT if (char.ToUpper(filter[pos]) == 'N' && char.ToUpper(filter[pos + 1]) == 'O' && char.ToUpper(filter[pos + 2]) == 'T' && char.IsWhiteSpace(filter[pos + 3])) { pos += 3; isNot = true; continue; } // IN else if (char.ToUpper(filter[pos]) == 'I' && char.ToUpper(filter[pos + 1]) == 'N' && (char.IsWhiteSpace(filter[pos + 2]) || filter[pos + 2] == '(')) { pos += 2; if (char.IsWhiteSpace(filter[pos])) // skip whitespace { while (pos < filter.Length && char.IsWhiteSpace(filter[pos])) { pos++; } if (filter[pos] != '(') { throwInvalidFilterConstruct(filter, pos - 2); } } comparisonOp = ComparisonOperatorEnum.In; } // REGEX else if (char.ToUpper(filter[pos]) == 'R' && char.ToUpper(filter[pos + 1]) == 'E' && char.ToUpper(filter[pos + 2]) == 'G' && char.ToUpper(filter[pos + 3]) == 'E' && char.ToUpper(filter[pos + 4]) == 'X' && char.IsWhiteSpace(filter[pos + 5])) { pos += 5; comparisonOp = ComparisonOperatorEnum.Regex; } // CONTAINS else if (char.ToUpper(filter[pos]) == 'C' && char.ToUpper(filter[pos + 1]) == 'O' && char.ToUpper(filter[pos + 2]) == 'N' && char.ToUpper(filter[pos + 3]) == 'T' && char.ToUpper(filter[pos + 4]) == 'A' && char.ToUpper(filter[pos + 5]) == 'I' && char.ToUpper(filter[pos + 6]) == 'N' && char.ToUpper(filter[pos + 7]) == 'S' && char.IsWhiteSpace(filter[pos + 8])) { pos += 8; comparisonOp = ComparisonOperatorEnum.Contains; } else { throwInvalidFilterConstruct(filter, pos - 2); } } catch (Exception ex) { throwInvalidFilterConstruct(filter, pos - 2); } } // alternative way to specify ignore case search (other way is to prefix a fieldname with ~) else if (filter[pos] == '~') // ~= { matchType = MatchTypeEnum.IgnoreCase; if (++pos >= filter.Length) { throwInvalidFilterConstruct(filter, pos); } // next char must be = if (filter[pos] != '=') { throwInvalidFilterConstruct(filter, pos); } comparisonOp = ComparisonOperatorEnum.Equal; } else if (filter[pos] == '!') // != { if (++pos >= filter.Length) { throwInvalidFilterConstruct(filter, pos); } // next char must be = if (filter[pos] != '=') { throwInvalidFilterConstruct(filter, pos); } comparisonOp = ComparisonOperatorEnum.Equal; isNot = true; } else if (filter[pos] == '=') { comparisonOp = ComparisonOperatorEnum.Equal; } else if (filter[pos] == '<') // <, <= or <> { if (pos + 1 >= filter.Length) { throwInvalidFilterConstruct(filter, pos); } if (filter[pos + 1] == '>') { pos++; comparisonOp = ComparisonOperatorEnum.Equal; isNot = true; } else if (filter[pos + 1] == '=') { pos++; comparisonOp = ComparisonOperatorEnum.LessThanOrEqual; } else { comparisonOp = ComparisonOperatorEnum.LessThan; } } else if (filter[pos] == '>') // > or >= { if (pos + 1 >= filter.Length) { throwInvalidFilterConstruct(filter, pos); } if (filter[pos + 1] == '=') { pos++; comparisonOp = ComparisonOperatorEnum.GreaterThanOrEqual; } else { comparisonOp = ComparisonOperatorEnum.GreaterThan; } } else { throwInvalidFilterConstruct(filter, pos); } pos++; state = ParseState.Right; } #endregion CompareOp ////////////////////////////////////////////////////////// #region Right else if (state == ParseState.Right) { if (comparisonOp == ComparisonOperatorEnum.In) //|| comparisonOp == EqualityEnum.NotIn ) { // skip whitespace while (pos < filter.Length && char.IsWhiteSpace(filter[pos])) { pos++; } // filter[pos] should look like this now: (val1, val2, val3) // or like this: ('val1', 'val2', 'val3') if (filter[pos] == '(') { pos++; } // find the end int endPos = pos; while (endPos < filter.Length && filter[endPos] != ')') { endPos++; } if (endPos >= filter.Length) { throw new FileDbException(string.Format(FileDbException.InvalidFilterConstruct, filter.Substring(pos)), FileDbExceptionsEnum.InvalidFilterConstruct); } string inVals = filter.Substring(pos, endPos - pos); searchVal = parseInVals(inVals, matchType); pos = endPos; } else { if (!inString) { // skip whitespace only if we haven't found anything yet if (sbTemp.Length == 0) { while (pos < filter.Length && char.IsWhiteSpace(filter[pos])) { pos++; } } // look for end of ExpressionGroup if (sbTemp.Length > 0 && (filter[pos] == ')' || char.IsWhiteSpace(filter[pos]))) { // Expression completed searchVal = sbTemp.ToString(); sbTemp.Length = 0; var srchExp = new FilterExpression(fieldName, searchVal, comparisonOp, matchType, isNot); parentSrchExpGrp.Add(curBoolOp, srchExp); if (filter[pos] == ')') { return; } fieldName = null; state = ParseState.BoolOp; } else if (sbTemp.Length == 0 && filter[pos] == '\'') { // just starting to get the value inString = /*isString=*/ true; } else { sbTemp.Append(filter[pos]); } } else // inString == true { if (filter[pos] == '\'') { //Debug.Assert( sbTemp.Length > 0 ); -- it could be empty, eg. myfield = '' // if the next char is NOT another ' (escaped) then the string is completed if ((pos + 1 < filter.Length) && filter[pos + 1] == '\'') { pos++; } else { inString = false; searchVal = sbTemp.ToString(); sbTemp.Length = 0; var srchExp = new FilterExpression(fieldName, searchVal, comparisonOp, matchType, isNot); parentSrchExpGrp.Add(curBoolOp, srchExp); fieldName = null; state = ParseState.BoolOp; goto Advance; } } sbTemp.Append(filter[pos]); } } Advance: // advance pos++; } #endregion Right ////////////////////////////////////////////////////////// #region Next else // if( state == ParseState.BoolOp ) { Debug.Assert(state == ParseState.BoolOp); if (sbTemp.Length == 0) { // skip whitespace while (pos < filter.Length && char.IsWhiteSpace(filter[pos])) { pos++; } } if (filter[pos] == ')') { return; // we must be finished } if (char.IsWhiteSpace(filter[pos])) { // we must be finished if (sbTemp.Length == 0) { throw new FileDbException(string.Format(FileDbException.InvalidFilterConstruct, filter.Substring(pos)), FileDbExceptionsEnum.InvalidFilterConstruct); } string sOp = sbTemp.ToString(); sbTemp.Length = 0; if (string.Compare(sOp, "AND", StringComparison.OrdinalIgnoreCase) == 0) { curBoolOp = BoolOpEnum.And; } else if (string.Compare(sOp, "OR", StringComparison.OrdinalIgnoreCase) == 0) { curBoolOp = BoolOpEnum.Or; } else { throw new FileDbException(string.Format(FileDbException.InvalidFilterConstruct, filter.Substring(pos)), FileDbExceptionsEnum.InvalidFilterConstruct); } state = ParseState.Left; // start over on next expression // skip whitespace while (pos < filter.Length && char.IsWhiteSpace(filter[pos])) { pos++; } // reset vars startPos = pos; hasBracket = false; } else { sbTemp.Append(filter[pos]); pos++; } } #endregion Next } // for... // did we just complete an Expression? if (state == ParseState.Right) { if (comparisonOp != ComparisonOperatorEnum.In) //&& comparisonOp != EqualityEnum.NotIn ) { searchVal = sbTemp.ToString(); if (!inString && string.Compare((string)searchVal, "null", StringComparison.OrdinalIgnoreCase) == 0) { searchVal = null; } sbTemp.Length = 0; } var srchExp = new FilterExpression(fieldName, searchVal, comparisonOp, matchType, isNot); parentSrchExpGrp.Add(curBoolOp, srchExp); } }
private static void Common(ROM rom, ParseState state) { }
private async Task AddmapperToDeck(ParseState state) { int totalSongs = 0; string mapperid = ""; var resp = await Plugin.WebClient.GetAsync($"https://beatsaver.com/api/search/text/q={state.parameter}", System.Threading.CancellationToken.None); if (resp.IsSuccessStatusCode) { JSONNode result = resp.ConvertToJsonNode(); if (result["docs"].IsArray && result["totalDocs"].AsInt == 0) { QueueChatMessage($"No results found for request \"{state.parameter}\""); return; } foreach (JSONObject song in result["docs"].AsArray) { mapperid = song["uploaderId"].Value; break; } if (mapperid == "") { QueueChatMessage($"Unable to find mapper {state.parameter}"); return; } } else { Plugin.Log($"Error {resp.ReasonPhrase} occured when trying to request song {state.parameter}!"); QueueChatMessage($"Invalid BeatSaver ID \"{state.parameter}\" specified."); return; } int offset = 0; string deckname = $"{state.parameter}.deck"; string requestUrl = "https://beatsaver.com/api/maps/uploader/"; bool found = true; while (found) { found = false; var mapperResp = await Plugin.WebClient.GetAsync($"{requestUrl}/{mapperid}/{offset}", System.Threading.CancellationToken.None); if (mapperResp.IsSuccessStatusCode) { JSONNode result = mapperResp.ConvertToJsonNode(); if (result["docs"].IsArray && result["totalDocs"].AsInt == 0) { QueueChatMessage($"No results found for request \"{state.parameter}\""); return; } JSONObject song; foreach (JSONObject entry in result["docs"]) { song = entry; // We ignore the duplicate filter for this listcollection.add(deckname, song["id"]); found = true; totalSongs++;; } } else { Plugin.Log($"Error {mapperResp.ReasonPhrase} occured when trying to request song {state.parameter}!"); QueueChatMessage($"Invalid BeatSaver ID \"{state.parameter}\" specified."); return; } offset += 1; } state.msg($"Added {totalSongs} to {deckname}."); }