public CsGlobalNamespace(CsPreprocess[] preprocess, CsBody body, CsAttribute[] attrs, CsExternAlias[] externs, CsUsingAlias[] aliases, CsUsingDirective[] uses, CsNamespace[] namespaces, CsType[] types, int length) : base(body, "<globals>", externs, aliases, uses, namespaces, types, 0, length, 1) { Contract.Requires(attrs != null, "attrs is null"); Contract.Requires(preprocess != null, "preprocess is null"); Attributes = attrs; Preprocess = preprocess; }
// event-declaration: // attributes? event-modifiers? event type variable-declarators ; // attributes? event-modifiers? event type member-name { event-accessor-declarations } // // variable-declarators: // variable-declarator // variable-declarators , variable-declarator private void DoParseEventDeclaration(List<CsMember> members, CsAttribute[] attrs, MemberModifiers modifiers, Token first) { string type = DoParseType(); Token next = m_scanner.LookAhead(1); if (next.IsPunct("=") || next.IsPunct(",") || next.IsPunct(";")) { while (true) { DoParseEventDeclarator(type, members, attrs, modifiers, first); if (m_scanner.Token.IsPunct(",")) m_scanner.Advance(); else break; } DoParsePunct(";"); } else { int nameOffset = m_scanner.Token.Offset; string name = DoParseMemberName(); Token last = m_scanner.Token; Token open = m_scanner.Token; DoSkipBody("{", "}", ref open, ref last); members.Add(new CsEvent(nameOffset, type, name, attrs, modifiers, first.Offset, last.Offset + last.Length - first.Offset, first.Line)); } }
// field-declaration: // attributes? field-modifiers? type variable-declarators ; private void DoParseFieldDeclaration(string type, List<CsMember> members, CsAttribute[] attrs, MemberModifiers modifiers, Token first) { while (true) { DoParseFieldDeclarator(type, members, attrs, modifiers, first); if (m_scanner.Token.IsPunct(",")) m_scanner.Advance(); else break; } DoParsePunct(";"); }
// operator-declaration: // attributes? operator-modifiers operator-declarator operator-body // // conversion-operator-declarator: // implicit operator type ( type identifier ) // explicit operator type ( type identifier ) private void DoParseConversionOperatorDeclaration(bool isImplicit, List<CsMember> members, CsAttribute[] attrs, MemberModifiers modifiers, Token first) { DoParseKeyword("operator"); int nameOffset = m_scanner.Token.Offset; string type = DoParseType(); var parms = new List<CsParameter>(); DoParsePunct("("); DoParseFormalParameterList(parms); DoParsePunct(")"); Token last = m_scanner.Token; CsBody body = null; if (m_scanner.Token.IsPunct(";")) { m_scanner.Advance(); } else { Token f = m_scanner.Token; Token start = m_scanner.Token; DoSkipBody("{", "}", ref f, ref last); body = new CsBody(isImplicit ? "op_Implict" : "op_Explict", start.Offset, f.Offset, last.Offset + last.Length - f.Offset, start.Line); } members.Add(new CsOperator(nameOffset, body, isImplicit, !isImplicit, parms.ToArray(), type, attrs, modifiers, type, first.Offset, last.Offset + last.Length - first.Offset, first.Line)); }
// destructor-declaration: // attributes? extern? ~ identifier ( ) destructor-body // // destructor-body: // block // ; private void DoParseDestructorDeclaration(List<CsMember> members, CsAttribute[] attrs, MemberModifiers modifiers, Token first) { Token last = first; int nameOffset = m_scanner.Token.Offset; string name = "~" + DoParseIdentifier(ref last); DoParsePunct("("); DoParsePunct(")"); last = m_scanner.Token; Token start = m_scanner.Token; Token open = new Token(); Token close = last; if (m_scanner.Token.IsPunct(";")) { m_scanner.Advance(); } else { DoSkipBody("{", "}", ref open, ref last); close = last; } CsBody body = open.Length > 0 ? new CsBody(name, start.Offset, open.Offset, close.Offset + close.Length - start.Offset, start.Line) : null; members.Add(new CsMethod(nameOffset, body, false, true, null, new CsParameter[0], null, "void", attrs, modifiers, name, first.Offset, last.Offset + last.Length - first.Offset, first.Line)); }
private void DoParseTypeDeclarationStub(Token first, CsAttribute[] attrs, MemberModifiers modifiers, List<CsType> types, MemberModifiers defaultAccess) { if (m_scanner.Token.IsIdentifier("enum")) { m_scanner.Advance(); CsType type = DoParseEnumDeclaration(attrs, modifiers, first, defaultAccess); types.Add(type); } else if (m_scanner.Token.IsIdentifier("delegate")) { m_scanner.Advance(); CsType type = DoParseDelegateDeclaration(attrs, modifiers, first, defaultAccess); types.Add(type); } else if (m_scanner.Token.IsIdentifier("interface") || (m_scanner.Token.IsIdentifier("partial") && m_scanner.LookAhead(1).IsIdentifier("interface"))) { CsType type = DoParseInterfaceDeclaration(attrs, modifiers, first, defaultAccess); types.Add(type); } else if (m_scanner.Token.IsIdentifier("struct") || (m_scanner.Token.IsIdentifier("partial") && m_scanner.LookAhead(1).IsIdentifier("struct"))) { CsType type = DoParseStructDeclaration(attrs, modifiers, first, defaultAccess); types.Add(type); } else if (m_scanner.Token.IsIdentifier("class") || (m_scanner.Token.IsIdentifier("partial") && m_scanner.LookAhead(1).IsIdentifier("class"))) { CsType type = DoParseClassDeclaration(attrs, modifiers, first, defaultAccess); types.Add(type); } else throw new CsParserException("Expected 'class', 'struct', 'interface', 'enum', 'delegate', or 'partial' on line {0}, but found '{1}'", m_scanner.Token.Line, m_scanner.Token.Text()); }
// class-declaration: // attributes? class-modifiers? partial? class identifier type-parameter-list? // class-base? type-parameter-constraints-clauses? class-body ;? private CsType DoParseClassDeclaration(CsAttribute[] attrs, MemberModifiers modifiers, Token first, MemberModifiers defaultAccess) { // partial? if (m_scanner.Token.IsIdentifier("partial")) { m_scanner.Advance(); modifiers |= MemberModifiers.Partial; } // class DoParseKeyword("class"); // identifier Token last = m_scanner.Token; int nameOffset = m_scanner.Token.Offset; string name = DoParseIdentifier(ref last); // type-parameter-list? string gargs = null; if (m_scanner.Token.IsPunct("<")) { gargs = DoScanBody("<", ">", ref last); } // class-base? CsBases bases = DoParseInterfaceTypeList(last); // type-parameter-constraints-clauses? string constraints = DoParseTypeParameterConstraintsClauses(); // class-body var members = new List<CsMember>(); var types = new List<CsType>(); Token open = m_scanner.Token; Token start = m_scanner.Token; DoParseStructBody(members, types, ref open, ref last); Token close = last; // ;? if (m_scanner.Token.IsPunct(";")) { last = m_scanner.Token; m_scanner.Advance(); } CsBody body = new CsBody(name, start.Offset, open.Offset, close.Offset + close.Length - start.Offset, start.Line); CsClass result = new CsClass(nameOffset, body, members.ToArray(), types.ToArray(), bases, constraints, gargs, attrs, modifiers, name, first.Offset, last.Offset + last.Length - first.Offset, first.Line); return result; }
public CsParameter(CsAttribute[] attrs, ParameterModifier modifier, bool isParams, string type, string name) { Contract.Requires(attrs != null, "attrs is null"); Contract.Requires(!string.IsNullOrEmpty(type), "type is null or empty"); Contract.Requires(!string.IsNullOrEmpty(name), "name is null or empty"); Attributes = attrs; Type = type.TrimAll(); Modifier = modifier; IsParams = isParams; Name = name; }
// indexer-declaration: // attributes? indexer-modifiers? indexer-declarator { accessor-declarations } // // indexer-declarator: // type this [ formal-parameter-list ] // type interface-type . this [ formal-parameter-list ] private void DoParseIndexerDeclaration(string type, string name, int nameOffset, List<CsMember> members, CsAttribute[] attrs, MemberModifiers modifiers, Token first) { var parms = new List<CsParameter>(); DoParsePunct("["); DoParseFormalParameterList(parms); DoParsePunct("]"); bool hasGet = false, hasSet = false; CsBody getterBody = null, setterBody = null; CsAttribute[] getAttrs = null, setAttrs = null; MemberModifiers getAccess = 0, setAccess = 0; DoParsePunct("{"); DoParseAccessorDeclarations(name, ref hasGet, ref hasSet, ref getterBody, ref setterBody, ref getAttrs, ref setAttrs, ref getAccess, ref setAccess); if (!m_scanner.Token.IsPunct("}")) DoParseAccessorDeclarations(name, ref hasGet, ref hasSet, ref getterBody, ref setterBody, ref getAttrs, ref setAttrs, ref getAccess, ref setAccess); Token last = DoParsePunct("}"); members.Add(new CsIndexer(nameOffset, getterBody, setterBody, getAccess, setAccess, name, getAttrs, setAttrs, hasGet, hasSet, parms.ToArray(), type, attrs, modifiers, first.Offset, last.Offset + last.Length - first.Offset, first.Line)); }
public CsMethod(int nameOffset, CsBody body, bool isCtor, bool isDtor, string constraints, CsParameter[] parms, string gargs, string rtype, CsAttribute[] attrs, MemberModifiers modifiers, string name, int offset, int length, int line) : base(nameOffset, attrs, modifiers, name, offset, length, line) { Contract.Requires(!(isCtor && isDtor), "can't be both a ctor and a dtor"); Contract.Requires(parms != null, "parms is null"); Contract.Requires(!string.IsNullOrEmpty(rtype), "rtype is null or empty"); Body = body; IsConstructor = isCtor; IsFinalizer = isDtor; ReturnType = rtype.TrimAll(); Parameters = parms; Constraints = constraints; if (gargs != null) GenericArguments = gargs.TrimAll(); }
public CsOperator(int nameOffset, CsBody body, bool isImplicit, bool isExplicit, CsParameter[] parms, string rtype, CsAttribute[] attrs, MemberModifiers modifiers, string name, int offset, int length, int line) : base(nameOffset, attrs, modifiers, name, offset, length, line) { Contract.Requires(!string.IsNullOrEmpty(rtype), "rtype is null or empty"); ReturnType = rtype.TrimAll(); Parameters = parms; IsImplicit = isImplicit; IsExplicit = isExplicit; Body = body; }
public static readonly int AccessMask = 0x000F; // can't define this in MemberModifiers or it messes up ToString #endregion Fields #region Constructors protected CsMember(int nameOffset, CsAttribute[] attrs, MemberModifiers modifiers, string name, int offset, int length, int line) : base(offset, length, line) { Contract.Requires(attrs != null, "attrs is null"); Contract.Requires(!string.IsNullOrEmpty(name), "name is null or empty"); Contract.Requires(nameOffset >= offset, "nameOffset is too small"); // usually the name will be after the member start, but it won't be for ctors with no modifiers Contract.Requires(nameOffset < offset + length, "nameOffset is too large"); Attributes = attrs; Modifiers = modifiers; Name = name; NameOffset = nameOffset; Contract.Ensures(Access != MemberModifiers.None, "access was not set"); }
public CsInterface(int nameOffset, CsBody body, CsMember[] members, CsType[] types, CsBases bases, string constraints, string gargs, CsAttribute[] attrs, MemberModifiers modifiers, string name, int offset, int length, int line) : base(nameOffset, body, bases, members, types, attrs, modifiers, constraints, gargs, name, offset, length, line) { }
public CsIndexer(int nameOffset, CsBody getterBody, CsBody setterBody, MemberModifiers getAccess, MemberModifiers setAccess, string name, CsAttribute[] getAttrs, CsAttribute[] setAttrs, bool hasGet, bool hasSet, CsParameter[] parms, string rtype, CsAttribute[] attrs, MemberModifiers modifiers, int offset, int length, int line) : base(nameOffset, attrs, modifiers, name, offset, length, line) { Contract.Requires(!hasGet || getAttrs != null, "getAttrs is null"); Contract.Requires(!hasSet || setAttrs != null, "setAttrs is null"); Contract.Requires(hasGet || hasSet, "not a getter and not a setter"); Contract.Requires(parms != null, "parms is null"); Contract.Requires(!string.IsNullOrEmpty(rtype), "rtype is null or empty"); Contract.Requires(((int) getAccess & ~CsMember.AccessMask) == 0, "getAccess has more than just acccess set"); Contract.Requires(((int) setAccess & ~CsMember.AccessMask) == 0, "setAccess has more than just acccess set"); HasGetter = hasGet; HasSetter = hasSet; ReturnType = rtype.TrimAll(); Parameters = parms; GetterAttributes = getAttrs; SetterAttributes = setAttrs; GetterAccess = getAccess; SetterAccess = setAccess; GetterBody = getterBody; SetterBody = setterBody; }
// operator-declaration: // attributes? operator-modifiers operator-declarator operator-body // // operator-declarator: // unary-operator-declarator // binary-operator-declarator // conversion-operator-declarator // // unary-operator-declarator: // type operator overloadable-unary-operator ( type identifier ) // // binary-operator-declarator: // type operator overloadable-binary-operator ( type identifier , type identifier ) // // operator-body: // block // ; private void DoParseOperatorDeclaration(string type, List<CsMember> members, CsAttribute[] attrs, MemberModifiers modifiers, Token first) { int nameOffset = m_scanner.Token.Offset; string name = DoParseOperatorName(); var parms = new List<CsParameter>(); DoParsePunct("("); DoParseFormalParameterList(parms); DoParsePunct(")"); Token last = m_scanner.Token; CsBody body = null; if (m_scanner.Token.IsPunct(";")) { m_scanner.Advance(); } else { Token f = m_scanner.Token; Token start = m_scanner.Token; DoSkipBody("{", "}", ref f, ref last); body = new CsBody(name, start.Offset, f.Offset, last.Offset + last.Length - f.Offset, start.Line); } members.Add(new CsOperator(nameOffset, body, false, false, parms.ToArray(), type, attrs, modifiers, name, first.Offset, last.Offset + last.Length - first.Offset, first.Line)); }
// interface-accessors: // attributes? get ; // attributes? set ; // attributes? get ; attributes? set ; // attributes? set ; attributes? get ; private void DoParseInterfaceAccessors(ref bool isGetter, ref bool isSetter, ref CsAttribute[] getAttrs, ref CsAttribute[] setAttrs) { CsAttribute[] attrs = DoParseAttributes(); if (m_scanner.Token.IsIdentifier("get")) { isGetter = true; getAttrs = attrs; } else if (m_scanner.Token.IsIdentifier("set")) { isSetter = true; setAttrs = attrs; } else throw new CsParserException("Expected 'get' or 'set' on line {0}, but found '{1}'", m_scanner.Token.Line, m_scanner.Token.Text()); m_scanner.Advance(); DoParsePunct(";"); }
// property-declaration: // attributes? property-modifiers? type member-name { accessor-declarations } private void DoParsePropertyDeclaration(string type, string name, int nameOffset, List<CsMember> members, CsAttribute[] attrs, MemberModifiers modifiers, Token first) { bool hasGet = false, hasSet = false; CsBody getterBody = null, setterBody = null; CsAttribute[] getAttrs = null, setAttrs = null; MemberModifiers getAccess = 0, setAccess = 0; DoParsePunct("{"); DoParseAccessorDeclarations(name, ref hasGet, ref hasSet, ref getterBody, ref setterBody, ref getAttrs, ref setAttrs, ref getAccess, ref setAccess); if (!m_scanner.Token.IsPunct("}")) DoParseAccessorDeclarations(name, ref hasGet, ref hasSet, ref getterBody, ref setterBody, ref getAttrs, ref setAttrs, ref getAccess, ref setAccess); Token last = DoParsePunct("}"); members.Add(new CsProperty(nameOffset, getterBody, setterBody, getAccess, setAccess, name, getAttrs, setAttrs, hasGet, hasSet, type, attrs, modifiers, first.Offset, last.Offset + last.Length - first.Offset, first.Line)); }
// interface-event-declaration: // attributes? new? event type identifier ; private void DoParseInterfaceEventDeclaration(List<CsMember> members, CsAttribute[] attrs, MemberModifiers modifiers, Token first) { Token last = m_scanner.Token; string type = DoParseType(); int nameOffset = m_scanner.Token.Offset; string name = DoParseIdentifier(ref last); last = DoParsePunct(";"); members.Add(new CsEvent(nameOffset, type, name, attrs, modifiers, first.Offset, last.Offset + last.Length - first.Offset, first.Line)); }
// accessor-declarations: // get-accessor-declaration set-accessor-declaration? // set-accessor-declaration get-accessor-declaration? // // get-accessor-declaration: // attributes? accessor-modifier? get accessor-body // // set-accessor-declaration: // attributes? accessor-modifier? set accessor-body // // accessor-body: // block // ; private void DoParseAccessorDeclarations(string name, ref bool isGetter, ref bool isSetter, ref CsBody getterBody, ref CsBody setterBody, ref CsAttribute[] getAttrs, ref CsAttribute[] setAttrs, ref MemberModifiers getAccess, ref MemberModifiers setAccess ) { CsAttribute[] attrs = DoParseAttributes(); MemberModifiers modifiers = DoParseModifiers(); Token last = m_scanner.Token; if (m_scanner.Token.IsIdentifier("get")) { isGetter = true; getAttrs = attrs; getAccess = modifiers; m_scanner.Advance(); if (m_scanner.Token.IsPunct(";")) { DoParsePunct(";"); } else { Token start = m_scanner.Token; Token first = m_scanner.Token; DoSkipBody("{", "}", ref first, ref last); getterBody = new CsBody("get_" + name, start.Offset, first.Offset, last.Offset + last.Length - start.Offset, start.Line); } } else if (m_scanner.Token.IsIdentifier("set")) { isSetter = true; setAttrs = attrs; setAccess = modifiers; m_scanner.Advance(); if (m_scanner.Token.IsPunct(";")) { DoParsePunct(";"); } else { Token first = m_scanner.Token; Token start = m_scanner.Token; DoSkipBody("{", "}", ref first, ref last); setterBody = new CsBody("set_" + name, start.Offset, first.Offset, last.Offset + last.Length - start.Offset, start.Line); } } else throw new CsParserException("Expected 'get' or 'set' on line {0}, but found '{1}'", m_scanner.Token.Line, m_scanner.Token.Text()); }
// interface-indexer-declaration: // attributes? new? type this [ formal-parameter-list ] { interface-accessors } private void DoParseInterfaceIndexerDeclaration(int nameOffset, List<CsMember> members, CsAttribute[] attrs, MemberModifiers modifiers, string rtype, Token first) { var parms = new List<CsParameter>(); DoParsePunct("["); DoParseFormalParameterList(parms); DoParsePunct("]"); bool hasGet = false, hasSet = false; CsAttribute[] getAttrs = null; CsAttribute[] setAttrs = null; DoParsePunct("{"); DoParseInterfaceAccessors(ref hasGet, ref hasSet, ref getAttrs, ref setAttrs); if (!m_scanner.Token.IsPunct("}")) DoParseInterfaceAccessors(ref hasGet, ref hasSet, ref getAttrs, ref setAttrs); Token last = DoParsePunct("}"); members.Add(new CsIndexer(nameOffset, null, null, 0, 0, "<this>", getAttrs, setAttrs, hasGet, hasSet, parms.ToArray(), rtype, attrs, modifiers, first.Offset, last.Offset + last.Length - first.Offset, first.Line)); }
// constructor-declaration: // attributes? constructor-modifiers? constructor-declarator constructor-body // // constructor-declarator: // identifier ( formal-parameter-list? ) constructor-initializer? // // constructor-initializer: // : base ( argument-list? ) // : this ( argument-list? ) // // constructor-body: // block // ; private void DoParseConstructorDeclaration(string name, int nameOffset, List<CsMember> members, CsAttribute[] attrs, MemberModifiers modifiers, Token first) { var parms = new List<CsParameter>(); DoParseFormalParameterList(parms); DoParsePunct(")"); string gargs = null; int i = name.IndexOf('<'); if (i > 0) { int j = name.IndexOf('>'); gargs = name.Substring(i + 1, j - (i + 1)).TrimAll(); name = name.Substring(0, i); } Token last = m_scanner.Token; Token open = last; if (m_scanner.Token.IsPunct(":")) { m_scanner.Advance(); DoParseIdentifier(ref last); DoSkipBody("(", ")", ref open, ref last); } last = m_scanner.Token; open = new Token(); Token start = m_scanner.Token; Token close = last; if (m_scanner.Token.IsPunct(";")) { m_scanner.Advance(); } else { DoSkipBody("{", "}", ref open, ref last); close = last; } CsBody body = open.Length > 0 ? new CsBody(name, start.Offset, open.Offset, close.Offset + close.Length - start.Offset, start.Line) : null; members.Add(new CsMethod(nameOffset, body, true, false, null, parms.ToArray(), gargs, "void", attrs, modifiers, name, first.Offset, last.Offset + last.Length - first.Offset, first.Line)); }
// interface-method-declaration: // attributes? new? return-type identifier type-parameter-list // ( formal-parameter-list? ) type-parameter-constraints-clauses? ; private void DoParseInterfaceMethodDeclaration(List<CsMember> members, CsAttribute[] attrs, MemberModifiers modifiers, string rtype, Token first) { Token last = m_scanner.Token; int nameOffset = m_scanner.Token.Offset; string name = DoParseIdentifier(ref last); string gargs = null; if (m_scanner.Token.IsPunct("<")) { gargs = DoScanBody("<", ">", ref last); } var parms = new List<CsParameter>(); DoParsePunct("("); DoParseFormalParameterList(parms); DoParsePunct(")"); string constraints = DoParseTypeParameterConstraintsClauses(); last = DoParsePunct(";"); members.Add(new CsMethod(nameOffset, null, false, false, constraints, parms.ToArray(), gargs, rtype, attrs, modifiers, name, first.Offset, last.Offset + last.Length - first.Offset, first.Line)); }
// delegate-declaration: // attributes? delegate-modifiers? delegate return-type identifier // type-parameter-list? // ( formal-parameter-list? ) type-parameter-constraints-clauses? ; // // type-parameter-list: // < type-parameters > private CsType DoParseDelegateDeclaration(CsAttribute[] attrs, MemberModifiers modifiers, Token first, MemberModifiers defaultAccess) { Token last = m_scanner.Token; string rtype = DoParseReturnType(); int nameOffset = m_scanner.Token.Offset; string name = DoParseIdentifier(ref last); string gargs = null; if (m_scanner.Token.IsPunct("<")) { gargs = DoScanBody("<", ">", ref last); } var parms = new List<CsParameter>(); DoParsePunct("("); DoParseFormalParameterList(parms); DoParsePunct(")"); string constraints = DoParseTypeParameterConstraintsClauses(); last = DoParsePunct(";"); if (((int) modifiers & CsMember.AccessMask) == 0) modifiers |= defaultAccess; // ;? if (m_scanner.Token.IsPunct(";")) { last = m_scanner.Token; m_scanner.Advance(); } return new CsDelegate(nameOffset, constraints, parms.ToArray(), gargs, rtype, attrs, modifiers, name, first.Offset, last.Offset + last.Length - first.Offset, first.Line); }
// interface-property-declaration: // attributes? new? type identifier { interface-accessors } private void DoParseInterfacePropertyDeclaration(List<CsMember> members, CsAttribute[] attrs, MemberModifiers modifiers, string rtype, Token first) { Token last = m_scanner.Token; int nameOffset = m_scanner.Token.Offset; string name = DoParseIdentifier(ref last); bool hasGet = false, hasSet = false; CsAttribute[] getAttrs = null; CsAttribute[] setAttrs = null; DoParsePunct("{"); DoParseInterfaceAccessors(ref hasGet, ref hasSet, ref getAttrs, ref setAttrs); if (m_scanner.Token.Kind == TokenKind.Identifier) DoParseInterfaceAccessors(ref hasGet, ref hasSet, ref getAttrs, ref setAttrs); last = DoParsePunct("}"); members.Add(new CsProperty(nameOffset, null, null, 0, 0, name, getAttrs, setAttrs, hasGet, hasSet, rtype, attrs, modifiers, first.Offset, last.Offset + last.Length - first.Offset, first.Line)); }
// enum-declaration: // attributes? enum-modifiers? enum identifier enum-base? enum-body ;? // // enum-base: // : integral-type // // enum-body: // { enum-member-declarations? } // { enum-member-declarations , } private CsType DoParseEnumDeclaration(CsAttribute[] attrs, MemberModifiers modifiers, Token first, MemberModifiers defaultAccess) { Token last = first; int nameOffset = m_scanner.Token.Offset; string name = DoParseIdentifier(ref last); string baseType = "int"; if (m_scanner.Token.IsPunct(":")) { m_scanner.Advance(); baseType = DoParseIdentifier(ref last); } DoParsePunct("{"); string[] names = DoParseEnumMemberDeclarations(); last = m_scanner.Token; DoParsePunct("}"); if (((int) modifiers & CsMember.AccessMask) == 0) modifiers |= defaultAccess; // ;? if (m_scanner.Token.IsPunct(";")) { last = m_scanner.Token; m_scanner.Advance(); } return new CsEnum(names, nameOffset, baseType, attrs, modifiers, name, first.Offset, last.Offset + last.Length - first.Offset, first.Line); }
// method-declaration: // method-header method-body // // method-header: // attributes? method-modifiers? partial? return-type member-name type-parameter-list? ( formal-parameter-list? ) type-parameter-constraints-clauses? private void DoParseMethodDeclaration(Token first, CsAttribute[] attrs, MemberModifiers modifiers, List<CsMember> members) { string rtype = DoParseReturnType(); int nameOffset = m_scanner.Token.Offset; string name = DoParseMemberName(); DoParseMethodStub(rtype, name, nameOffset, first, attrs, modifiers, members); }
// variable-declarator: // identifier // identifier = variable-initializer private void DoParseEventDeclarator(string type, List<CsMember> members, CsAttribute[] attrs, MemberModifiers modifiers, Token first) { Token last = m_scanner.Token; int nameOffset = m_scanner.Token.Offset; string name = DoParseIdentifier(ref last); if (m_scanner.Token.IsPunct("=")) { DoParsePunct("="); // TODO: this won't parse multiple declarators correctly. We could probably handle this // by scanning until we hit a semi-colon or a comma not within brackets. while (m_scanner.Token.IsValid() && !m_scanner.Token.IsPunct(";")) { last = m_scanner.Token; m_scanner.Advance(); } } members.Add(new CsEvent(nameOffset, type, name, attrs, modifiers, first.Offset, last.Offset + last.Length - first.Offset, first.Line)); }
// method-header: // ... type-parameter-list? ( formal-parameter-list? ) type-parameter-constraints-clauses? // // method-body: // block // ; private void DoParseMethodStub(string rtype, string memberName, int nameOffset, Token first, CsAttribute[] attrs, MemberModifiers modifiers, List<CsMember> members) { Token last = m_scanner.Token; string gargs = null; int i = memberName.IndexOf('<'); if (i > 0) { int j = memberName.IndexOf('>'); gargs = memberName.Substring(i + 1, j - (i + 1)).TrimAll(); memberName = memberName.Substring(0, i); } var parms = new List<CsParameter>(); DoParsePunct("("); DoParseFormalParameterList(parms); DoParsePunct(")"); string constraints = DoParseTypeParameterConstraintsClauses(); Token open = new Token(); Token start = m_scanner.Token; Token close = m_scanner.Token; if (m_scanner.Token.IsPunct(";")) { m_scanner.Advance(); } else { DoSkipBody("{", "}", ref open, ref last); close = last; } CsBody body = open.Length > 0 ? new CsBody(memberName, start.Offset, open.Offset, close.Offset + close.Length - start.Offset, start.Line) : null; members.Add(new CsMethod(nameOffset, body, false, false, constraints, parms.ToArray(), gargs, rtype, attrs, modifiers, memberName, first.Offset, last.Offset + last.Length - first.Offset, first.Line)); }
// constant-declarator: // identifier = constant-expression // // field-declaration: // attributes? field-modifiers? type variable-declarators ; // // variable-declarator: // identifier // identifier = variable-initializer private void DoParseFieldDeclarator(string type, List<CsMember> members, CsAttribute[] attrs, MemberModifiers modifiers, Token first) { Token last = m_scanner.Token; int nameOffset = m_scanner.Token.Offset; string name = DoParseIdentifier(ref last); string value = null; if (m_scanner.Token.IsPunct("=")) { DoParsePunct("="); value = DoParseExpression(ref last, ";"); } members.Add(new CsField(nameOffset, type, value, attrs, modifiers, name, first.Offset, last.Offset + last.Length - first.Offset, first.Line)); }
public CsField(int nameOffset, string type, string value, CsAttribute[] attrs, MemberModifiers modifiers, string name, int offset, int length, int line) : base(nameOffset, attrs, modifiers, name, offset, length, line) { Contract.Requires(!string.IsNullOrEmpty(type), "type is null or empty"); Contract.Requires(value == null || value.Length > 0, "value is empty"); Type = type.TrimAll(); Value = value; }