public AltRule(SymbolPosition pos, string markWith, IEnumerable <RhsGroup> groups, CodeMix gen) { this.Position = pos; this.MarkWith = markWith; // [@PARSER_USER_ACTION] // here we swap the notion of user action // if in grammar user didn't pass ANY code -- it means identity function // if the grammar user pass empty code -- it means passing null value, so as shorcut set null as entire user action if (gen == null) // if there was no code at all, we infer the end object { CodeMix mix = null; if (inferEndObject(groups, ref mix)) { this.Code = mix; } else { throw ParseControlException.NewAndRun("Couldn't infer which object to pass at " + pos.XYString()); } } // if there was an empty code, such code does not produce anything (i.e. null) // thus we set entire action as null -- it is a shortcut else if (gen.IsEmpty) { this.Code = null; } else { this.Code = gen; } SetGroups(groups); }
private CodeMix(SymbolPosition position, string comment) { this.Position = position; this.Comment = comment; this.identifiers = new HashSet <string>(); this.elements = new List <IMixedBody>(); }
private IEnumerable <AltRule> createSeedAndAppend(SymbolPosition position, List <Production> productions, bool doubleSeed, string lhsSymbolName, IEnumerable <string> elementTypeNames, params RhsSymbol[] symbols) { yield return(createSeed(position, productions, doubleSeed, lhsSymbolName, elementTypeNames, symbols)); yield return(createAppend(position, lhsSymbolName, elementTypeNames, symbols)); }
public RhsSymbol(SymbolPosition coords, string userLabel, string symbolName, IEnumerable <string> tabooSymbols, bool marked, bool skip) : base(userLabel, symbolName) { this.Coords = coords; this.SkipInitially = skip; this.IsMarked = marked; this.TabooSymbols = (tabooSymbols ?? new string[] { }).ToList(); this.CombinedSymbols = null; }
private static AltRule createSubstitution(SymbolPosition position, string substSymbolName) { var code = new CodeBody().AddIdentifier("obj"); return(AltRule.CreateInternally(position, new RhsGroup[] { RhsGroup.CreateSequence(position, RepetitionEnum.Once, new RhsSymbol(position, "obj", substSymbolName)) }, new CodeMix(CodeMix.SubstitutionComment).AddBody(code))); }
internal MatchInfo(STATE_ENUM state, SymbolPosition position, Rule <SYMBOL_ENUM, STATE_ENUM> rule, string text) { this.StateIn = state; this.Position = position; this.Rule = rule; this.Text = text; this.tokens = new List <TokenMatch <SYMBOL_ENUM> >(); this.stateActions = new List <string>(); }
// multiple entities repeated are created not as list of tuples, but tuple of the lists // e.g. (a b)+ translates into Tuple<List(a),List(b)> private CodeBody makeTupleListCode(SymbolPosition position, IEnumerable <string> elementTypeNames) { if (elementTypeNames.Any()) { return(makeTupleListCode(elementTypeNames)); } else { reportError(position.XYString() + " Cannot infer which elements have to be aggregated with repetition"); return(new CodeBody()); } }
internal ProductionInfo(SymbolPosition pos, string lhsSymbol, RecursiveEnum recursive, IEnumerable <RhsSymbol> rhsSymbols, string altMarkWith) { this.Position = pos; this.LhsSymbol = lhsSymbol; this.Recursive = recursive; this.rhsSymbols = rhsSymbols.ToList(); this.TabooSymbols = rhsSymbols.Select(it => it.TabooSymbols).ToArray(); this.PassedMarkedWith = altMarkWith; this.EffectiveMarkedWith = altMarkWith ?? rhsSymbols.Where(it => it.IsMarked).Select(it => it.SymbolName).SingleOrDefault(); this.IdentityOuterFunctionParamIndex = NoIdentityFunction; }
protected override void SetSymbolPositions() { SymbolPosition.Add('a', new Point(0, 0)); SymbolPosition.Add('b', new Point(16, 0)); SymbolPosition.Add('c', new Point(32, 0)); SymbolPosition.Add('d', new Point(48, 0)); SymbolPosition.Add('e', new Point(64, 0)); SymbolPosition.Add('f', new Point(80, 0)); SymbolPosition.Add('g', new Point(96, 0)); SymbolPosition.Add('h', new Point(112, 0)); SymbolPosition.Add('i', new Point(128, 0)); SymbolPosition.Add('j', new Point(144, 0)); SymbolPosition.Add('k', new Point(160, 0)); SymbolPosition.Add('l', new Point(176, 0)); SymbolPosition.Add('m', new Point(192, 0)); SymbolPosition.Add('n', new Point(208, 0)); SymbolPosition.Add('o', new Point(224, 0)); SymbolPosition.Add('p', new Point(240, 0)); // Second Row SymbolPosition.Add('q', new Point(0, 32)); SymbolPosition.Add('r', new Point(16, 32)); SymbolPosition.Add('s', new Point(32, 32)); SymbolPosition.Add('t', new Point(48, 32)); SymbolPosition.Add('u', new Point(64, 32)); SymbolPosition.Add('v', new Point(80, 32)); SymbolPosition.Add('w', new Point(96, 32)); SymbolPosition.Add('x', new Point(112, 32)); SymbolPosition.Add('y', new Point(128, 32)); SymbolPosition.Add('z', new Point(144, 32)); // Third Row SymbolPosition.Add('0', new Point(0, 64)); SymbolPosition.Add('1', new Point(16, 64)); SymbolPosition.Add('2', new Point(32, 64)); SymbolPosition.Add('3', new Point(48, 64)); SymbolPosition.Add('4', new Point(64, 64)); SymbolPosition.Add('5', new Point(80, 64)); SymbolPosition.Add('6', new Point(96, 64)); SymbolPosition.Add('7', new Point(112, 64)); SymbolPosition.Add('8', new Point(128, 64)); SymbolPosition.Add('9', new Point(144, 64)); }
private bool SyntaxError(ITokenMatch <int> cur_token, List <string> errors) { SymbolPosition coords = cur_token.Coordinates.FirstPosition; customErrors.Add("\"" + filename + "\", at (" + coords.Line + "," + coords.Column + "): syntax error at or near " + cur_token.Text); omerrs++; if (omerrs > 50) { customErrors.Add("More than 50 errors"); return(false); } return(true); }
private static AltRule createAppend(SymbolPosition position, string lhsSymbolName, IEnumerable <string> elementTypeNames, params RhsSymbol[] symbols) { // if it does not exists it means we don't care about adding it IEnumerable <RhsSymbol> named_symbols = symbols.Where(it => it.ObjName != null).ToArray(); string list_obj_name; if (named_symbols.Count() == 1) { list_obj_name = "list"; } else { list_obj_name = "tuple_list"; } // since we are creating this code, the only conflict can come from symbol object names list_obj_name = Grammar.RegisterName(list_obj_name, named_symbols.Select(it => it.ObjName).ToList()); var code = new CodeBody().AddSnippet("{"); if (named_symbols.Count() == 1) { code.AddWithIdentifier(list_obj_name, ".", "Add", "(", named_symbols.Single().ObjName, ");"); } else { foreach (Tuple <RhsSymbol, int> sym_pair in named_symbols.ZipWithIndex()) { code.AddWithIdentifier(list_obj_name, ".", CodeWords.Item(sym_pair.Item2 + 1), ".", "Add", "("); code.AddIdentifier(sym_pair.Item1.ObjName); code.AddSnippet(");"); } } code.AddWithIdentifier("return", " ", list_obj_name, ";}"); return(AltRule.CreateInternally(position, new RhsGroup[] { RhsGroup.CreateSequence(position, RepetitionEnum.Once, new RhsSymbol(position, list_obj_name, lhsSymbolName)), RhsGroup.CreateSequence(position, RepetitionEnum.Once, symbols.Select(it => it.ShallowClone().SetSkip(false)).ToArray()) }, new CodeMix(CodeMix.AppendComment).AddBody(code))); }
private RhsGroup addListProduction(SymbolPosition position, List <Production> productions, string lhsSymbolName, RhsGroup group, string elementObjName, IEnumerable <string> elementTypeNames) { if (!group.CanBeMultiplied) { throw new ArgumentException("Internal error"); } if (group.Repetition.In(RepetitionEnum.OneOrMore, RepetitionEnum.TwoOrMore, RepetitionEnum.NullOrMany)) { return(RhsGroup.CreateSequence(position, group.Repetition == RepetitionEnum.NullOrMany? RepetitionEnum.Option: RepetitionEnum.Once, new RhsSymbol(position, elementObjName, lhsSymbolName))); } // can be empty production else if (group.Repetition.In(RepetitionEnum.EmptyOrMany)) { // we don't use another approach -- making a list an optional element -- because it would mean // a list or no list, and this approach translates always to a list, just either filled or empty // it is a difference as null or empty list -- and X* with repetition 0 should produce empty list, // not null string opt_lhs_typename = makeTupleListCode(position, elementTypeNames).Make(); string opt_lhs_symbol_name = grammar.RegisterNewSymbol(AutoNames.OptList + elementObjName + "__", opt_lhs_typename); productions.Add(Production.CreateInternally( new SymbolInfo(opt_lhs_symbol_name, opt_lhs_typename), new AltRule[] { createSeed(position, productions, false, opt_lhs_symbol_name, elementTypeNames /*, no symbols*/), // empty seed createSubstitution(position, lhsSymbolName) })); return(RhsGroup.CreateSequence(position, RepetitionEnum.Once, new RhsSymbol(position, elementObjName, opt_lhs_symbol_name))); } else if (group.Repetition.In(RepetitionEnum.Once, RepetitionEnum.Option)) { throw new ArgumentException("Internal error"); } else { throw new NotImplementedException(); } }
public long IncreaseActualPosition(SymbolInfo symbol, long increase) { using (positionsLocker.Using()) { SymbolPosition symbolPosition; if (!positions.TryGetValue(symbol.BinaryIdentifier, out symbolPosition)) { symbolPosition = new SymbolPosition { Position = increase }; positions.Add(symbol.BinaryIdentifier, symbolPosition); } else { symbolPosition.Position += increase; } return(symbolPosition.Position); } }
public void SetActualPosition(SymbolInfo symbol, long position) { using (positionsLocker.Using()) { if (debug) { log.Debug("SetActualPosition( " + symbol + " = " + position + ")"); } SymbolPosition symbolPosition; if (!positions.TryGetValue(symbol.BinaryIdentifier, out symbolPosition)) { symbolPosition = new SymbolPosition { Position = position }; positions.Add(symbol.BinaryIdentifier, symbolPosition); } else { positions[symbol.BinaryIdentifier].Position = position; } } }
/// <summary> /// Tokenises the specified string /// </summary> /// <param name="input"></param> /// <returns></returns> public IEnumerable<Symbol> Tokenise(string input) { int ptr = 0; SymbolPosition currentPos = new SymbolPosition(1, 0); while (ptr < input.Length) { bool found = false; for (int i = 0; i < tokenInfoList.Count; i++) { TokenInfo tokenInfo = tokenInfoList[i]; Match match = tokenInfo.Pattern.Match(input, ptr); if (match.Success && match.Index == ptr) { string segment = input.Substring(match.Index, match.Length); if (!tokenInfo.Discard) yield return new Symbol(tokenInfo.Token, segment, currentPos); ptr = match.Index + match.Length; int numNewLines = 0, newCol = currentPos.Column; for (int j = 0; j < segment.Length; j++) { if (segment[j] == '\n') { numNewLines++; newCol = 0; } else if (segment[j] == '\t') newCol += TAB_SIZE; else if (segment[j] != '\r') newCol++; } currentPos = new SymbolPosition(currentPos.Line + numNewLines, newCol); found = true; break; } } if (!found) throw new Exception(string.Format("Unrecognised symbol '{0}'", StringUtils.Escape(input.Substring(ptr, 1)))); } yield break; }
private RhsGroup unfoldMultiplicatedGroup(SymbolPosition position, string objName, IEnumerable <string> objTypes, List <Production> outProductions, RhsGroup group, params IEnumerable <RhsSymbol>[] symbols) { if (!group.CanBeMultiplied) { throw new ArgumentException("Internal error"); } string lhs_typename = makeTupleListCode(position, objTypes).Make(); string lhs_symbol_name = grammar.RegisterNewSymbol(AutoNames.List + objName + "__", lhs_typename); outProductions.Add(Production.CreateInternally(new SymbolInfo(lhs_symbol_name, lhs_typename), symbols.Select(ss => createSeedAndAppend(position, outProductions, group.Repetition == RepetitionEnum.TwoOrMore, // double initially lhs_symbol_name, objTypes, ss.ToArray())) .Flatten())); return(addListProduction(position, outProductions, lhs_symbol_name, group, objName, objTypes)); }
private RhsGroup(SymbolPosition position, IEnumerable <IRhsEntity> chunks, RepetitionEnum repetition, ModeEnum mode) : this(position, null, chunks, repetition, mode) { }
public static RhsGroup CreateSet(SymbolPosition position, string name, IEnumerable <IRhsEntity> chunks, RepetitionEnum repetition) { return(new RhsGroup(position, name, chunks, repetition, ModeEnum.Set)); }
public static RhsGroup CreateSet(SymbolPosition position, IEnumerable <IRhsEntity> chunks, RepetitionEnum repetition) { return(CreateSet(position, null, chunks, repetition)); }
public static RhsGroup CreateAltogether(SymbolPosition position, string name, IEnumerable <IRhsEntity> chunks, RepetitionEnum repetition) { return(new RhsGroup(position, name, chunks, repetition, ModeEnum.Altogether)); //return RhsGroup.CreateSet(position, name, chunks.Concat(RhsGroup.CreateSequence(position,RepetitionEnum.Once,chunks.ToArray())), repetition); }
public static RhsGroup CreateSequence(SymbolPosition pos, string name, RepetitionEnum repetition, params IRhsEntity[] symbols) { return(new RhsGroup(pos, name, symbols, repetition, ModeEnum.Sequence)); }
public static RhsGroup CreateSequence(SymbolPosition pos, RepetitionEnum repetition, params IRhsEntity[] symbols) { return(CreateSequence(pos, null, repetition, symbols)); }
private bool SnapshotLoadLast(Snapshot snapshot) { try { uniqueIds.Clear(); replaceIds.Clear(); originalIds.Clear(); memory.Position = snapshot.Offset + sizeof(Int32); // Skip the snapshot length; remoteSequence = reader.ReadInt32(); localSequence = reader.ReadInt32(); lastSequenceReset = new TimeStamp(reader.ReadInt64()); int orderCount = reader.ReadInt32(); for (var i = 0; i < orderCount; i++) { var id = reader.ReadInt32(); var action = (OrderAction)reader.ReadInt32(); var brokerOrder = reader.ReadInt64(); var logicalOrderId = reader.ReadInt32(); var logicalSerialNumber = reader.ReadInt64(); var orderState = (OrderState)reader.ReadInt32(); var price = reader.ReadDouble(); var flags = (OrderFlags)reader.ReadInt32(); var replaceId = reader.ReadInt32(); var originalId = reader.ReadInt32(); var side = (OrderSide)reader.ReadInt32(); var size = reader.ReadInt32(); var symbol = reader.ReadString(); var tag = reader.ReadString(); if (string.IsNullOrEmpty(tag)) { tag = null; } var type = (OrderType)reader.ReadInt32(); var utcCreateTime = new TimeStamp(reader.ReadInt64()); var lastStateChange = new TimeStamp(reader.ReadInt64()); var sequence = reader.ReadInt32(); var symbolInfo = Factory.Symbol.LookupSymbol(symbol); var order = Factory.Utility.PhysicalOrder(action, orderState, symbolInfo, side, type, flags, price, size, logicalOrderId, logicalSerialNumber, brokerOrder, tag, utcCreateTime); order.ResetLastChange(lastStateChange); order.Sequence = sequence; uniqueIds.Add(id, order); if (replaceId != 0) { replaceIds.Add(id, replaceId); } if (originalId != 0) { originalIds.Add(id, originalId); } } foreach (var kvp in replaceIds) { var orderId = kvp.Key; var replaceId = kvp.Value; uniqueIds[orderId].ReplacedBy = uniqueIds[replaceId]; } foreach (var kvp in originalIds) { var orderId = kvp.Key; var originalId = kvp.Value; uniqueIds[orderId].OriginalOrder = uniqueIds[originalId]; } ordersByBrokerId.Clear(); ordersBySequence.Clear(); ordersBySerial.Clear(); foreach (var kvp in uniqueIds) { var order = kvp.Value; ordersByBrokerId[order.BrokerOrder] = order; ordersBySequence[order.Sequence] = order; if (order.Action == OrderAction.Cancel && order.OriginalOrder == null) { throw new ApplicationException("Cancel order w/o any original order setting: " + order); } } var bySerialCount = reader.ReadInt32(); for (var i = 0; i < bySerialCount; i++) { var logicalSerialNum = reader.ReadInt64(); var orderId = reader.ReadInt32(); var order = uniqueIds[orderId]; ordersBySerial[order.LogicalSerialNumber] = order; } using (positionsLocker.Using()) { var positionCount = reader.ReadInt32(); positions = new Dictionary <long, SymbolPosition>(); for (var i = 0L; i < positionCount; i++) { var symbolString = reader.ReadString(); var symbol = Factory.Symbol.LookupSymbol(symbolString); var position = reader.ReadInt64(); var symbolPosition = new SymbolPosition { Position = position }; positions.Add(symbol.BinaryIdentifier, symbolPosition); } strategyPositions = new Dictionary <int, StrategyPosition>(); } return(true); } catch (Exception ex) { log.Info("Loading snapshot at offset " + snapshot.Offset + " failed due to " + ex.Message + ". Rolling back to previous snapshot.", ex); return(false); } }
public RhsSymbol(SymbolPosition coords, string userLabel, string symbolName) : this(coords, userLabel, symbolName, tabooSymbols : null, marked : false) { }
private AltRule createSeed(SymbolPosition position, List <Production> productions, bool doubled, // should we double the seed right at the start string lhsSymbolName, IEnumerable <string> elementTypeNames, params RhsSymbol[] symbols) { RhsSymbol[] init_symbols = symbols.Where(it => !it.SkipInitially).ToArray(); var main_code = new CodeBody().AddWithIdentifier(CodeWords.New, " "); main_code.Append(makeTupleListCode(position, elementTypeNames)); main_code.AddSnippet("("); IEnumerable <CodeBody> code_lists = elementTypeNames.Select(it => new CodeBody().AddWithIdentifier(CodeWords.New, " ", CodeWords.List, "<", it, ">{")).ToList(); // purpose: to handle names in doubled seed mode Dictionary <RhsSymbol, string[]> obj_name_substs = new Dictionary <RhsSymbol, string[]>(); { IEnumerable <RhsSymbol> named_symbols = symbols.Where(it => it.ObjName != null); if (named_symbols.Any()) { foreach (Tuple <RhsSymbol, CodeBody> tuple in named_symbols.SyncZip(code_lists)) { RhsSymbol nsymbol = tuple.Item1; CodeBody lcode = tuple.Item2; bool double_sym = doubled && !nsymbol.SkipInitially; string[] name_subst = new string[double_sym ? 2 : 1]; obj_name_substs.Add(nsymbol, name_subst); // if we double the element, we have to come up with new names if (double_sym) { name_subst[0] = AutoNames.Double1 + nsymbol.ObjName + "__"; name_subst[1] = AutoNames.Double2 + nsymbol.ObjName + "__"; } else { name_subst[0] = nsymbol.ObjName; } for (int i = 0; i < (double_sym ? 2 : 1); ++i) { if (i == 1) { lcode.AddSnippet(","); } if (double_sym) { lcode.AddIdentifier(name_subst[i]); } else { lcode.AddIdentifier(nsymbol.ObjName); } } } } } foreach (Tuple <CodeBody, int> code_pair in code_lists.ZipWithIndex()) { code_pair.Item1.AddSnippet("}"); if (code_pair.Item2 > 0) { main_code.AddSnippet(","); } main_code.Append(code_pair.Item1); } main_code.AddSnippet(")"); // in case of doubled seed we have to rename the symbols // otherwise just make shallow copies without renaming // but since we already set proxy name correctly, we can use shared code for both cases var seed_symbols = new LinkedList <RhsSymbol>(); for (int i = 0; i < (doubled ? 2 : 1); ++i) { foreach (RhsSymbol sym in (i == 0 ? init_symbols : symbols)) { if (sym.ObjName == null) { seed_symbols.AddLast(sym.ShallowClone()); } else { int s_idx = (i == 1 && sym.SkipInitially) ? 0 : i; seed_symbols.AddLast(sym.Renamed(obj_name_substs[sym][s_idx])); } } } return(AltRule.CreateInternally(position, // are there any symbols for production seed_symbols.Any() ? new RhsGroup[] { RhsGroup.CreateSequence(position, RepetitionEnum.Once, seed_symbols.Select(it => it.SetSkip(false)).ToArray()) } : new RhsGroup[] { }, new CodeMix(CodeMix.SeedComment).AddBody(main_code))); }
public RhsSymbol(SymbolPosition coords, string userLabel, string symbolName, IEnumerable <string> tabooSymbols, bool marked) : this(coords, userLabel, symbolName, tabooSymbols, marked : marked, skip : false) { }
internal static AltRule CreateInternally(SymbolPosition pos, IEnumerable <RhsGroup> groups, CodeMix code) { return(new AltRule(pos, null, groups, code)); }
public CodeMix(SymbolPosition position) : this(position, null) { }