本文整理汇总了C#中System.Xml.Linq.XElement.FirstElement方法的典型用法代码示例。如果您正苦于以下问题:C# XElement.FirstElement方法的具体用法?C# XElement.FirstElement怎么用?C# XElement.FirstElement使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Xml.Linq.XElement
的用法示例。
在下文中一共展示了XElement.FirstElement方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateIterationStatement
public static UnifiedExpression CreateIterationStatement(XElement node)
{
Contract.Requires(node != null);
Contract.Requires(node.Name() == "iteration_statement");
/*
iteration_statement
: 'while' '(' expression ')' statement
| 'do' statement 'while' '(' expression ')' ';'
| 'for' '(' expression_statement expression_statement expression? ')' statement
*/
var first = node.FirstElement().Value;
var body =
UnifiedBlock.Create(
CreateStatement(node.FirstElement("statement")));
switch (first) {
case "while":
return UnifiedWhile.Create(CreateExpression(node.NthElement(2)), body);
case "do":
return UnifiedDoWhile.Create(CreateExpression(node.NthElement(4)), body);
case "for":
var step = node.Element("expression");
var stepExp = step != null ? CreateExpression(step) : null;
return UnifiedFor.Create(
CreateExpressionStatement(node.NthElement(2)),
CreateExpressionStatement(node.NthElement(3)),
stepExp, body);
default:
throw new InvalidOperationException();
}
}
示例2: CreateAlias
public static UnifiedExpression CreateAlias(XElement node)
{
Contract.Requires(node != null);
Contract.Requires(node.Name() == "alias");
return UnifiedAlias.Create(
CreateExpresion(node.FirstElement()),
CreateExpresion(node.LastElement()));
}
示例3: CreateLit
public static UnifiedExpression CreateLit(XElement node)
{
Contract.Requires(node != null);
Contract.Requires(node.Name() == "lit");
var child = node.FirstElement();
switch (child.Name()) {
case "Symbol":
return UnifiedSymbolLiteral.Create(child.Value);
}
return CreateExpresion(child);
}
示例4: CreateAsgn
public static UnifiedBinaryExpression CreateAsgn(XElement node)
{
Contract.Requires(node != null);
Contract.Requires(
node.Name() == "lasgn" || node.Name() == "masgn"
|| node.Name() == "iasgn"
|| node.Name() == "gasgn" || node.Name() == "cvdecl"
|| node.Name() == "cdecl");
return UnifiedBinaryExpression.Create(
CreateExpresion(node.FirstElement()),
UnifiedBinaryOperator.Create(
"=", UnifiedBinaryOperatorKind.Assign),
CreateExpresion(node.LastElement()));
}
示例5: CreateExpressionStatement
public static UnifiedExpression CreateExpressionStatement(
XElement node)
{
Contract.Requires(node != null);
Contract.Requires(node.Name() == "expression_statement");
/*
expression_statement
: ';'
| expression ';'
*/
var first = node.FirstElement();
return first.Name == "expression" ? CreateExpression(first) : null;
}
示例6: CreateCompilationUnit
public static UnifiedProgram CreateCompilationUnit(XElement node) {
Contract.Requires(node != null);
Contract.Requires(node.Name() == "compilationUnit");
/*
* compilationUnit
* : ( (annotations)? packageDeclaration )? (importDeclaration)* (typeDeclaration)*
*/
var program = UnifiedProgram.Create(UnifiedBlock.Create());
var expressions = program.Body;
var first = node.FirstElementOrDefault();
if (first.SafeName() == "annotations") {
var annotations = CreateAnnotations(node.FirstElement());
var packageDeclaration =
CreatePackageDeclaration(node.NthElement(1));
packageDeclaration.Annotations = annotations;
expressions.Add(packageDeclaration);
expressions = packageDeclaration.Body;
} else if (first.SafeName() == "packageDeclaration") {
var packageDeclaration = CreatePackageDeclaration(first);
expressions.Add(packageDeclaration);
expressions = packageDeclaration.Body;
}
foreach (var e in node.Elements("importDeclaration")) {
var importDeclaration = CreateImportDeclaration(e);
expressions.Add(importDeclaration);
}
foreach (var e in node.Elements("typeDeclaration")) {
var typeDeclaration = CreateTypeDeclaration(e);
expressions.AddRange(typeDeclaration);
}
// Pick up comment nodes which are in last children of the root node
program.Comments = node.Elements(Code2XmlConstants.CommentName)
.Select(CreateComment)
.ToSet();
return program;
}
示例7: CreateExternalDeclaration
public static UnifiedExpression CreateExternalDeclaration(
XElement node)
{
Contract.Requires(node != null);
Contract.Requires(node.Name() == "external_declaration");
/*
* external_declaration
* options {k=1;}
* : ( declaration_specifiers? declarator declaration* '{' )=> function_definition
* | declaration
* ;
*/
// 前者が関数宣言のこと、後者がtypedefのこと
var first = node.FirstElement();
if (first.Name() == "function_definition") {
return CreateFunctionDefinition(first);
}
if (first.Name() == "declaration") {
return CreateDeclaration(first);
}
throw new InvalidOperationException();
}
示例8: CreateWhenAndDefault
private static IEnumerable<UnifiedCase> CreateWhenAndDefault(
XElement node)
{
Contract.Requires(node != null);
if (node.Name() == "nil") {
yield break;
}
if (node.Name() != "when") {
yield return UnifiedCase.CreateDefault(CreateSmartBlock(node));
} else {
var first = node.FirstElement();
var caseConds = first.Elements()
.Select(CreateExpresion)
.ToList();
int i;
for (i = 0; i < caseConds.Count - 1; i++) {
yield return UnifiedCase.Create(caseConds[i]);
}
yield return
UnifiedCase.Create(
caseConds[i],
CreateSmartBlock(node.LastElement()));
}
}
示例9: CreateDot3
private static UnifiedExpression CreateDot3(XElement node)
{
Contract.Requires(node != null);
Contract.Requires(node.Name() == "dot3");
return UnifiedRange.Create(
CreateExpresion(node.FirstElement()),
CreateExpresion(node.LastElement()));
}
示例10: CreateIf
private static UnifiedIf CreateIf(XElement node) {
Contract.Requires(node != null);
Contract.Requires(node.Name() == "statement");
Contract.Requires(node.FirstElement().Name() == "IF");
/*
* 'if' parExpression statement ('else' statement)?
*/
var trueBody = CreateStatement(node.Element("statement"))
.ToBlock();
UnifiedBlock falseBody = null;
if (node.Elements("statement").Count() == 2) {
var falseNode = node.Elements("statement").ElementAt(1);
falseBody = CreateStatement(falseNode).ToBlock();
}
return UnifiedIf.Create(
CreateParExpression(node.Element("parExpression")), trueBody,
falseBody);
}
示例11: CreateLasgnOrMasgnOrNil
public static IEnumerable<UnifiedVariableIdentifier> CreateLasgnOrMasgnOrNil(
XElement node)
{
Contract.Requires(node != null);
Contract.Requires(
node.Name() == "lasgn" || node.Name() == "masgn"
|| node.Name() == "nil");
Contract.Requires(
node.Name() == "nil" || node.Elements().Count() == 1);
Contract.Requires(
node.Name() == "nil" || node.Name() != "masgn"
|| node.FirstElement().Name() == "array");
return node.Descendants("Symbol")
.Select(CreateSymbol);
}
示例12: CreateStructOrUnionSpecifier
public static UnifiedType CreateStructOrUnionSpecifier(XElement node)
{
Contract.Requires(node != null);
Contract.Requires(node.Name() == "struct_or_union_specifier");
/* struct_or_union_specifier
* : struct_or_union IDENTIFIER? '{' struct_declaration_list '}'
* | struct_or_union IDENTIFIER
*/
// 構造体の定義と宣言の両方をこのメソッドで作成
// 常に UnifiedType を返すが、
// 構造体定義をしている場合だけ関数の呼び出し元で UnifiedType の中身をとりだす
// typedef "struct {}" data; -> クラス?
// "struct data {}"; -> クラス
// "struct data" a; -> 型
var isStruct = CreateStructOrUnion(node.FirstElement()) == "struct";
var identifier = node.Element("IDENTIFIER");
var typeName = identifier == null
? null
: UnifiedVariableIdentifier.Create(
identifier.Value);
// 型の場合
if (node.Elements().Count() == 2) {
var baseType = UnifiedType.Create(typeName);
return isStruct ? baseType.WrapStruct() : baseType.WrapUnion();
}
// struct or union の定義がある場合
var body =
CreateStructDeclarationList(
node.Element("struct_declaration_list"));
var structOrUnion = isStruct
? (UnifiedClassLikeDefinition)
UnifiedStructDefinition.
Create(
name: typeName,
body: body)
: UnifiedUnionDefinition.Create(
name: typeName, body: body);
// TODO struct or unionはあくまでもTypeとして返すののか?
return UnifiedType.Create(structOrUnion);
}
示例13: CreateTypeSpecifier
public static UnifiedExpression CreateTypeSpecifier(XElement node)
{
Contract.Requires(node != null);
Contract.Requires(node.Name() == "type_specifier");
/* type_specifier
* : 'void'
* | 'char'
* | 'short'
* | 'int'
* | 'long'
* | 'float'
* | 'double'
* | 'signed'
* | 'unsigned'
* | struct_or_union_specifier
* | enum_specifier
* | type_id
*/
var first = node.FirstElement();
switch (first.Name()) {
case "struct_or_union_specifier":
return CreateStructOrUnionSpecifier(first);
case "enum_specifier":
return CreateEnumSpecifier(first);
case "type_id":
return CreateTypeId(first);
default:
return
UnifiedType.Create(
UnifiedVariableIdentifier.Create(first.Value));
}
}
示例14: CreateLiteral
public static UnifiedLiteral CreateLiteral(XElement node) {
Contract.Requires(node != null);
Contract.Requires(node.Name() == "literal");
/*
* literal
* : intLiteral
* | longLiteral
* | floatLiteral
* | doubleLiteral
* | charLiteral
* | stringLiteral
* | trueLiteral
* | falseLiteral
* | nullLiteral
*/
var first = node.FirstElement();
switch (first.Name()) {
case "intLiteral":
return CreateIntLiteral(first);
case "longLiteral":
return CreateLongLiteral(first);
case "floatLiteral":
return CreateFloatLiteral(first);
case "doubleLiteral":
return CreateDoubleLiteral(first);
case "charLiteral":
return UnifiedCharLiteral.Create(first.Value);
case "stringLiteral":
return UnifiedStringLiteral.Create(first.Value);
case "trueLiteral":
return UnifiedBooleanLiteral.Create(true);
case "falseLiteral":
return UnifiedBooleanLiteral.Create(false);
case "nullLiteral":
return UnifiedNullLiteral.Create();
}
throw new InvalidOperationException();
}
示例15: CreateParameterDeclaration
public static UnifiedParameter CreateParameterDeclaration(XElement node)
{
Contract.Requires(node != null);
Contract.Requires(node.Name() == "parameter_declaration");
/*
parameter_declaration
: declaration_specifiers (declarator|abstract_declarator)*
*/
UnifiedIdentifier name;
UnifiedSet<UnifiedParameter> parameters;
var modifiersAndType =
CreateDeclarationSpecifiers(
node.Element("declaration_specifiers"));
var modifiers = modifiersAndType.Item1;
var type = (UnifiedType)modifiersAndType.Item2;
var declarators = node.Elements("declarator");
if (declarators.Count() == 0) {
return UnifiedParameter.Create(null, modifiers, type);
}
var declarator = CreateDeclarator(node.FirstElement("declarator"));
// abstract_declaratorはおそらく[]など=> よって、最初に現れることはないはず( // TODO 未検証)
if (node.Element("abstract_declarator") != null) {
// TODO (int y[])の場合は、[]はどこに付くのか?
throw new NotImplementedException();
}
// TODO 実際のところ、そこまで理解しきれていない
else if (declarators.Count() == 1) {
// 多分declarator自体は1つしか現れないはず( // TODO 未検証)
return UnifiedParameter.Create(
null, modifiers, type, declarator.Item1.ToSet());
} else if (node.Element("declarator") != null) {
parameters = declarator.Item2;
name = declarator.Item1;
if (parameters != null && parameters.Count > 0) {
// この場合はパラメータが関数ポインタ
var returnType = type;
type = UnifiedType.Create(
UnifiedFunctionDefinition.Create(
null, modifiers, returnType, null, null,
parameters));
modifiers = null;
}
return UnifiedParameter.Create(
null, modifiers, type, name.ToSet(), null);
}
throw new InvalidOperationException();
}