// Generates a codedom enumeration and attaches it to the given namespace. public static void Emit(CodeNamespace codeNamespace, Pie.Expressions.Enum e) { // CodeTypeDeclaration is the CodeDOM representation of a // class, struct, or enum. var codeTypeDeclaration = new CodeTypeDeclaration(); codeNamespace.Types.Add(codeTypeDeclaration); // Assign the unqualified name (without namespace). codeTypeDeclaration.Name = e.UnqualifiedName; // Flag the type as an enum. codeTypeDeclaration.IsEnum = true; // Set the accessibility of the enum. switch (e.Accessibility) { case Accessibility.Internal: codeTypeDeclaration.TypeAttributes = TypeAttributes.NestedAssembly; break; case Accessibility.Public: codeTypeDeclaration.TypeAttributes = TypeAttributes.Public; break; } // Translate the list of constants in the enum foreach (var c in e.Constants) { var f = new CodeMemberField(e.UnqualifiedName, c.Name); f.InitExpression = new CodePrimitiveExpression(c.Value); codeTypeDeclaration.Members.Add(f); } }
// Interpret the modifiers for this enum declaration. static void InterpretModifiers(Root root, Pie.Expressions.Enum c, ParseTreeNode node) { bool foundPublic = false; bool foundInternal = false; foreach (var modifierNode in node.ChildNodes) { string text = modifierNode.FindTokenAndGetText(); if (text == "public") { if (foundPublic || foundInternal) { root.CompilerErrors.Add(new MultipleAccessModifiersCompilerError("", modifierNode.FindToken().Location.Line, modifierNode.FindToken().Location.Position)); } else { c.Accessibility = Accessibility.Public; foundPublic = true; } } if (text == "internal") { if (foundInternal || foundPublic) { root.CompilerErrors.Add(new MultipleAccessModifiersCompilerError("", modifierNode.FindToken().Location.Line, modifierNode.FindToken().Location.Position)); } else { c.Accessibility = Accessibility.Internal; foundInternal = true; } } } }
// Build an enum expression public static void BuildEnum(IronyParser parser, Root root, Expression parentExpression, ParseTreeNode currentNode) { Pie.Expressions.Enum e = new Pie.Expressions.Enum(parentExpression, currentNode.Token.Convert()); parentExpression.ChildExpressions.Add(e); int i = 0; // Find modifiers InterpretModifiers(root, e, currentNode.ChildNodes[i]); i+=2; e.UnqualifiedName = currentNode.ChildNodes[i].FindTokenAndGetText(); i+=1; int enumValue = 0; // Get the constants defined by this enum: check that they have values assigned. if(currentNode.ChildNodes[i].ChildNodes.Count > 0) { foreach(var node in currentNode.ChildNodes[i].ChildNodes[0].ChildNodes) { if(node.ChildNodes[0].Term.ToString() == "identifier_constant") { e.Constants.Add(new EnumConstant(node.ChildNodes[0].FindTokenAndGetText(), enumValue++)); } if (node.ChildNodes[0].Term.ToString() == "assignment_constant") { enumValue = Int32.Parse(node.ChildNodes[0].ChildNodes[2].FindTokenAndGetText()); e.Constants.Add(new EnumConstant(node.ChildNodes[0].FindTokenAndGetText(), enumValue++)); } } } }
// Build an enum expression public static void BuildEnum(IronyParser parser, Root root, Expression parentExpression, ParseTreeNode currentNode) { Pie.Expressions.Enum e = new Pie.Expressions.Enum(parentExpression, currentNode.Token.Convert()); parentExpression.ChildExpressions.Add(e); int i = 0; // Find modifiers InterpretModifiers(root, e, currentNode.ChildNodes[i]); i += 2; e.UnqualifiedName = currentNode.ChildNodes[i].FindTokenAndGetText(); i += 1; int enumValue = 0; // Get the constants defined by this enum: check that they have values assigned. if (currentNode.ChildNodes[i].ChildNodes.Count > 0) { foreach (var node in currentNode.ChildNodes[i].ChildNodes[0].ChildNodes) { if (node.ChildNodes[0].Term.ToString() == "identifier_constant") { e.Constants.Add(new EnumConstant(node.ChildNodes[0].FindTokenAndGetText(), enumValue++)); } if (node.ChildNodes[0].Term.ToString() == "assignment_constant") { enumValue = Int32.Parse(node.ChildNodes[0].ChildNodes[2].FindTokenAndGetText()); e.Constants.Add(new EnumConstant(node.ChildNodes[0].FindTokenAndGetText(), enumValue++)); } } } }