public NonLocalAttrRef(StructDecl ctx, string ruleName, string name, int ruleIndex) : base(ctx) { this.name = name; this.ruleName = ruleName; this.ruleIndex = ruleIndex; }
public override bool Equals(object obj) { StructDecl other = obj as StructDecl; if (other == null) { return(false); } int elementCount = Elements.Length; if (elementCount != other.Elements.Length) { return(false); } for (int i = 0; i < elementCount; i++) { if (!Elements[i].Equals(other.Elements[i])) { return(false); } } return(Type.Equals(other.Type)); }
StructType AddStructType(StructDecl decl) { var name = decl.Name.ToLower(); Raise <TypeCheckException> .If(_types.ContainsKey(name)); const string nmsp = "DanglingLang.Runner"; const TypeAttributes typeAttr = TypeAttributes.Sealed | TypeAttributes.Public | TypeAttributes.AnsiClass | TypeAttributes.BeforeFieldInit | TypeAttributes.SequentialLayout; var typeDef = new TypeDefinition(nmsp, name, typeAttr, Module.Import(typeof(object))); var type = new StructType(name, typeDef); foreach (var f in decl.Fields) { var fieldType = GetType(f.Item2); Raise <TypeCheckException> .IfAreEqual("void", fieldType.Name, "Field cannot be void"); type.AddField(f.Item1, fieldType); } _types.Add(name, type); _structTypes.Add(name, type); return(type); }
public SetNonLocalAttr(StructDecl ctx, string ruleName, string name, int ruleIndex, IList <ActionChunk> rhsChunks) : base(ctx, name, rhsChunks) { this.ruleName = ruleName; this.ruleIndex = ruleIndex; }
public void AddStruct(Lexeme nameToken, StructDecl node, ErrorHandler errorHandler) { if (FindStructName(nameToken) != null) { errorHandler.AddMessage(Severity.Error, $"Duplicate declaration of struct '{nameToken.Value}'", nameToken); return; } Structs.Add(nameToken.Value, node); }
public Action(OutputModelFactory factory, StructDecl ctx, string action) : base(factory, null) { ActionAST ast = new ActionAST(new CommonToken(ANTLRParser.ACTION, action)); RuleFunction rf = factory.GetCurrentRuleFunction(); if (rf != null) { // we can translate ast.resolver = rf.rule; chunks = ActionTranslator.TranslateActionChunk(factory, rf, action, ast); } else { chunks = new List <ActionChunk>(); chunks.Add(new ActionText(ctx, action)); } }
// Structs containing non-equal structs are also non-equal private void CompareStruct(StructDecl iter, bool[] _structChanged) { for (int i = 0; i < _structs.Length; i++) { StructDecl curStruct = _structs[i]; if (curStruct != iter && !_structChanged[i]) { foreach (ElementDecl element in curStruct.Elements) { if (curStruct.Type == iter.Type && element.NameInfo.IsPointer) { _structChanged[i] = true; CompareStruct(curStruct, _structChanged); } } } } }
public LeftRecursiveRuleFunction(OutputModelFactory factory, LeftRecursiveRule r) : base(factory, r) { // Since we delete x=lr, we have to manually add decls for all labels // on left-recur refs to proper structs foreach (System.Tuple <GrammarAST, string> pair in r.leftRecursiveRuleRefLabels) { GrammarAST idAST = pair.Item1; string altLabel = pair.Item2; string label = idAST.Text; GrammarAST rrefAST = (GrammarAST)idAST.Parent.GetChild(1); if (rrefAST.Type == ANTLRParser.RULE_REF) { Rule targetRule = factory.GetGrammar().GetRule(rrefAST.Text); string ctxName = factory.GetTarget().GetRuleFunctionContextStructName(targetRule); RuleContextDecl d; if (idAST.Parent.Type == ANTLRParser.ASSIGN) { d = new RuleContextDecl(factory, label, ctxName); } else { d = new RuleContextListDecl(factory, label, ctxName); } StructDecl @struct = ruleCtx; if (altLabelCtxs != null) { AltLabelStructDecl s; if (altLabel != null && altLabelCtxs.TryGetValue(altLabel, out s) && s != null) { @struct = s; // if alt label, use subctx } } @struct.AddDecl(d); // stick in overall rule's ctx } } }
public void Visit(StructDecl structDecl) { var type = structDecl.Type; Indent(); _sb.Append("struct "); _sb.Append(type.Name); _sb.Append(" {"); int i = 0; for (; i < type.Fields.Count - 1; ++i) { _sb.Append(type.Fields[i].Type); _sb.Append(" "); _sb.Append(type.Fields[i].Name); _sb.Append("; "); } _sb.Append(type.Fields[i].Type); _sb.Append(" "); _sb.Append(type.Fields[i].Name); _sb.Append(";}\r\n"); }
public bool[] Compare(Dna memoryDna) { bool[] _structChanged = new bool[_structs.Length]; for (int i = 0; i < _structs.Length; i++) { StructDecl oldStruct = _structs[i]; StructDecl curStruct = memoryDna.GetStruct(oldStruct.Type.Name); _structChanged[i] = !_structs[i].Equals(curStruct); } // Recurse in for (int i = 0; i < _structs.Length; i++) { if (_structChanged[i]) { CompareStruct(_structs[i], _structChanged); } } return(_structChanged); }
public RulePropertyRef_text(StructDecl ctx, string label) : base(ctx, label) { }
public LabelRef(StructDecl ctx, string name) : base(ctx) { this.name = name; }
public void Visit(StructDecl structDecl) { // Nothing to do here... }
private void Init(BulletReader reader, bool swap) { if (swap) { throw new NotImplementedException(); } Stream stream = reader.BaseStream; reader.ReadTag("SDNA"); // Element names reader.ReadTag("NAME"); string[] names = reader.ReadStringList(); var nameInfos = names .Select(n => new NameInfo(n)) .ToArray(); _hasIntType = names.Contains("int"); // Types reader.ReadTag("TYPE"); string[] typeNames = reader.ReadStringList(); stream.Position = (stream.Position + 3) & ~3; reader.ReadTag("TLEN"); TypeDecl[] types = typeNames.Select(name => { short length = reader.ReadInt16(); if (_ptrLen == 0 && name == "ListBase") { _ptrLen = length / 2; } return new TypeDecl(name, length); }).ToArray(); stream.Position = (stream.Position + 3) & ~3; // Structs reader.ReadTag("STRC"); int numStructs = reader.ReadInt32(); _structs = new StructDecl[numStructs]; _structByTypeName = new Dictionary<string, StructDecl>(numStructs); for (int i = 0; i < numStructs; i++) { short typeIndex = reader.ReadInt16(); TypeDecl structType = types[typeIndex]; int numElements = reader.ReadInt16(); var elements = new ElementDecl[numElements]; for (int j = 0; j < numElements; j++) { typeIndex = reader.ReadInt16(); short nameIndex = reader.ReadInt16(); elements[j] = new ElementDecl(types[typeIndex], nameInfos[nameIndex]); } var structDecl = new StructDecl(structType, elements); structType.Struct = structDecl; _structs[i] = structDecl; _structByTypeName.Add(structType.Name, structDecl); } }
StructType AddStructType(StructDecl decl) { var name = decl.Name.ToLower(); Raise<TypeCheckException>.If(_types.ContainsKey(name)); const string nmsp = "DanglingLang.Runner"; const TypeAttributes typeAttr = TypeAttributes.Sealed | TypeAttributes.Public | TypeAttributes.AnsiClass | TypeAttributes.BeforeFieldInit | TypeAttributes.SequentialLayout; var typeDef = new TypeDefinition(nmsp, name, typeAttr, Module.Import(typeof(object))); var type = new StructType(name, typeDef); foreach (var f in decl.Fields) { var fieldType = GetType(f.Item2); Raise<TypeCheckException>.IfAreEqual("void", fieldType.Name, "Field cannot be void"); type.AddField(f.Item1, fieldType); } _types.Add(name, type); _structTypes.Add(name, type); return type; }
public QRetValueRef(StructDecl ctx, string dict, string name) : base(ctx, name) { this.dict = dict; }
public TokenPropertyRef(StructDecl ctx, string label) : base(ctx) { this.label = label; }
public void Visit(StructDecl structDecl) { structDecl.Type = AddStructType(structDecl); }
public ActionText(StructDecl ctx, string text) : base(ctx) { this.text = text; }
public void Visit(StructDecl structDecl) { // Each field is added to the struct type... const FieldAttributes fieldAttr = FieldAttributes.Public; var typeDef = structDecl.Type.Reference as TypeDefinition; Debug.Assert(typeDef != null); foreach (var f in structDecl.Type.Fields) { var fieldDef = new FieldDefinition(f.Name, fieldAttr, f.Type.Reference); typeDef.Fields.Add(fieldDef); f.Reference = fieldDef; } // We add a proper constructor to the new type. We first create // a parameter for each field in the struct. var structFields = structDecl.Type.Fields; var parameters = new ParameterDefinition[structFields.Count]; for (var i = 0; i < structFields.Count; ++i) { var pName = "_" + structFields[i].Name; const ParameterAttributes pAttr = ParameterAttributes.None; var pType = structFields[i].Type.Reference; parameters[i] = new ParameterDefinition(pName, pAttr, pType); } // Then, we create the constructor itself... const MethodAttributes ctorAttr = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName; var ctor = new MethodDefinition(".ctor", ctorAttr, _module.TypeSystem.Void); // And we add the parameters we created before. foreach (var p in parameters) { ctor.Parameters.Add(p); } // Then, we build a constructor so that each field receives // its new value from the corresponding parameter. ctor.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_0)); // This ctor.Body.Instructions.Add(Instruction.Create(OpCodes.Call, _objCtor)); for (var i = 0; i < parameters.Length; ++i) { ctor.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_0)); // This ctor.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg, parameters[i])); ctor.Body.Instructions.Add(Instruction.Create(OpCodes.Stfld, structFields[i].Reference)); } ctor.Body.Instructions.Add(Instruction.Create(OpCodes.Ret)); // Constructor is added to the new type. typeDef.Methods.Add(ctor); structDecl.Type.Ctor = ctor; // After that, we have to follow a similar procedure // to declare a proper "Equals" method for the new type. // As before, we first create the parameter. var eqParam = new ParameterDefinition("other", ParameterAttributes.None, typeDef); // Then, we create the method itself... const MethodAttributes eqAttr = MethodAttributes.Public | MethodAttributes.HideBySig; var equals = new MethodDefinition("MyEquals", eqAttr, _module.TypeSystem.Boolean); // And we add the parameters we created before. equals.Parameters.Add(eqParam); // Then, we build a method so that each field is compared // against value from the corresponding other field. var nop = Instruction.Create(OpCodes.Nop); var ret = Instruction.Create(OpCodes.Ret); foreach (var f in structFields) { equals.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_0)); // This equals.Body.Instructions.Add(Instruction.Create(OpCodes.Ldfld, f.Reference)); equals.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_1)); // Other equals.Body.Instructions.Add(Instruction.Create(OpCodes.Ldfld, f.Reference)); if (f.Type is StructType) { var fieldEq = (f.Type as StructType).TypeEquals; equals.Body.Instructions.Add(Instruction.Create(OpCodes.Call, fieldEq)); } else { equals.Body.Instructions.Add(Instruction.Create(OpCodes.Ceq)); } equals.Body.Instructions.Add(Instruction.Create(OpCodes.Brfalse, nop)); } equals.Body.Instructions.Add(Instruction.Create(OpCodes.Ldc_I4_1)); equals.Body.Instructions.Add(Instruction.Create(OpCodes.Br, ret)); equals.Body.Instructions.Add(nop); equals.Body.Instructions.Add(Instruction.Create(OpCodes.Ldc_I4_0)); equals.Body.Instructions.Add(ret); // Equals is added to the new type. typeDef.Methods.Add(equals); structDecl.Type.TypeEquals = equals; // New type is then added to the assembly. _module.Types.Add(typeDef); }
public ListLabelRef(StructDecl ctx, string name) : base(ctx, name) { }
public ActionChunk(StructDecl ctx) { this.ctx = ctx; }
public ActionTemplate(StructDecl ctx, Template st) : base(ctx) { this.st = st; }
public SetAttr(StructDecl ctx, string name, IList <ActionChunk> rhsChunks) : base(ctx) { this.name = name; this.rhsChunks = rhsChunks; }
public Action(OutputModelFactory factory, StructDecl ctx, Template actionST) : base(factory, null) { chunks = new List <ActionChunk>(); chunks.Add(new ActionTemplate(ctx, actionST)); }
public RetValueRef(StructDecl ctx, string name) : base(ctx) { this.name = name; }
public TokenRef(StructDecl ctx, string name) : base(ctx) { this.name = name; }
public ArgRef(StructDecl ctx, string name) : base(ctx, name) { }
public TokenPropertyRef_int(StructDecl ctx, string label) : base(ctx, label) { }
public void Init(BinaryReader reader, bool swap) { Stream stream = reader.BaseStream; // SDNA byte[] code = reader.ReadBytes(8); string codes = ASCIIEncoding.ASCII.GetString(code); // NAME if (!codes.Equals("SDNANAME")) { throw new InvalidDataException(); } int dataLen = reader.ReadInt32(); _names = new NameInfo[dataLen]; for (int i = 0; i < dataLen; i++) { List<byte> name = new List<byte>(); byte ch = reader.ReadByte(); while (ch != 0) { name.Add(ch); ch = reader.ReadByte(); } _names[i] = new NameInfo(ASCIIEncoding.ASCII.GetString(name.ToArray())); } stream.Position = (stream.Position + 3) & ~3; // TYPE code = reader.ReadBytes(4); codes = ASCIIEncoding.ASCII.GetString(code); if (!codes.Equals("TYPE")) { throw new InvalidDataException(); } dataLen = reader.ReadInt32(); _types = new TypeDecl[dataLen]; for (int i = 0; i < dataLen; i++) { List<byte> name = new List<byte>(); byte ch = reader.ReadByte(); while (ch != 0) { name.Add(ch); ch = reader.ReadByte(); } string type = ASCIIEncoding.ASCII.GetString(name.ToArray()); _types[i] = new TypeDecl(type); } stream.Position = (stream.Position + 3) & ~3; // TLEN code = reader.ReadBytes(4); codes = ASCIIEncoding.ASCII.GetString(code); if (!codes.Equals("TLEN")) { throw new InvalidDataException(); } for (int i = 0; i < _types.Length; i++) { _types[i].Length = reader.ReadInt16(); } stream.Position = (stream.Position + 3) & ~3; // STRC code = reader.ReadBytes(4); codes = ASCIIEncoding.ASCII.GetString(code); if (!codes.Equals("STRC")) { throw new InvalidDataException(); } dataLen = reader.ReadInt32(); _structs = new StructDecl[dataLen]; long shtPtr = stream.Position; for (int i = 0; i < dataLen; i++) { StructDecl structDecl = new StructDecl(); _structs[i] = structDecl; if (swap) { throw new NotImplementedException(); } else { short typeNr = reader.ReadInt16(); structDecl.Type = _types[typeNr]; structDecl.Type.Struct = structDecl; int numElements = reader.ReadInt16(); structDecl.Elements = new ElementDecl[numElements]; for (int j = 0; j < numElements; j++) { typeNr = reader.ReadInt16(); short nameNr = reader.ReadInt16(); structDecl.Elements[j] = new ElementDecl(_types[typeNr], _names[nameNr]); } } } // build reverse lookups _structReverse = new Dictionary<string, int>(_structs.Length); for (int i = 0; i < _structs.Length; i++) { StructDecl s = _structs[i]; if (_ptrLen == 0 && s.Type.Name.Equals("ListBase")) { _ptrLen = s.Type.Length / 2; } _structReverse.Add(s.Type.Name, i); } }
public ThisRulePropertyRef_start(StructDecl ctx, string label) : base(ctx, label) { }
public RuleFunction(OutputModelFactory factory, Rule r) : base(factory) { this.name = r.name; this.rule = r; if (r.modifiers != null && r.modifiers.Count > 0) { this.modifiers = new List <string>(); foreach (GrammarAST t in r.modifiers) { modifiers.Add(t.Text); } } modifiers = Utils.NodesToStrings(r.modifiers); index = r.index; int lfIndex = name.IndexOf(ATNSimulator.RuleVariantDelimiter); if (lfIndex >= 0) { variantOf = name.Substring(0, lfIndex); } if (r.name.Equals(r.GetBaseContext())) { ruleCtx = new StructDecl(factory, r); AddContextGetters(factory, r.g.contextASTs[r.name]); if (r.args != null) { ICollection <Attribute> decls = r.args.attributes.Values; if (decls.Count > 0) { args = new List <AttributeDecl>(); ruleCtx.AddDecls(decls); foreach (Attribute a in decls) { args.Add(new AttributeDecl(factory, a)); } ruleCtx.ctorAttrs = args; } } if (r.retvals != null) { ruleCtx.AddDecls(r.retvals.attributes.Values); } if (r.locals != null) { ruleCtx.AddDecls(r.locals.attributes.Values); } } else { if (r.args != null || r.retvals != null || r.locals != null) { throw new System.NotSupportedException("customized fields are not yet supported for customized context objects"); } } ruleLabels = r.GetElementLabelNames(); tokenLabels = r.GetTokenRefs(); if (r.exceptions != null) { exceptions = new List <ExceptionClause>(); foreach (GrammarAST e in r.exceptions) { ActionAST catchArg = (ActionAST)e.GetChild(0); ActionAST catchAction = (ActionAST)e.GetChild(1); exceptions.Add(new ExceptionClause(factory, catchArg, catchAction)); } } startState = factory.GetGrammar().atn.ruleToStartState[r.index]; }
private void Init(BulletReader reader, bool swap) { if (swap) { throw new NotImplementedException(); } Stream stream = reader.BaseStream; reader.ReadTag("SDNA"); // Element names reader.ReadTag("NAME"); string[] names = reader.ReadStringList(); var nameInfos = names .Select(n => new NameInfo(n)) .ToArray(); _hasIntType = names.Contains("int"); // Types reader.ReadTag("TYPE"); string[] typeNames = reader.ReadStringList(); stream.Position = (stream.Position + 3) & ~3; reader.ReadTag("TLEN"); TypeDecl[] types = typeNames.Select(name => { short length = reader.ReadInt16(); if (_ptrLen == 0 && name == "ListBase") { _ptrLen = length / 2; } return(new TypeDecl(name, length)); }).ToArray(); stream.Position = (stream.Position + 3) & ~3; // Structs reader.ReadTag("STRC"); int numStructs = reader.ReadInt32(); _structs = new StructDecl[numStructs]; _structByTypeName = new Dictionary <string, StructDecl>(numStructs); for (int i = 0; i < numStructs; i++) { short typeIndex = reader.ReadInt16(); TypeDecl structType = types[typeIndex]; int numElements = reader.ReadInt16(); var elements = new ElementDecl[numElements]; for (int j = 0; j < numElements; j++) { typeIndex = reader.ReadInt16(); short nameIndex = reader.ReadInt16(); elements[j] = new ElementDecl(types[typeIndex], nameInfos[nameIndex]); } var structDecl = new StructDecl(structType, elements); structType.Struct = structDecl; _structs[i] = structDecl; _structByTypeName.Add(structType.Name, structDecl); } }