Parse a document type declaration.

<pre>
[28] doctypedecl ::= '&lt;!DOCTYPE' S Name (S ExternalID)? S?
('[' (markupdecl | PEReference | S)* ']' S?)? '&gt;'
</pre>

<p>
(The <code>&lt;!DOCTYPE</code> has already been read.)
424045
private void parseDoctypedecl() throws Exception {
        String rootName;
        ExternalIdentifiers ids;

        // Read the document type name.
        requireWhitespace();
        rootName = readNmtoken(true);

        // Read the External subset's IDs
        skipWhitespace();
        ids = readExternalIds(false, true);

        // report (a) declaration of name, (b) lexical info (ids)
        handler.doctypeDecl(rootName, ids.publicId, ids.systemId);

        // Internal subset is parsed first, if present
        skipWhitespace();
        if (tryRead('[')) {

            // loop until the subset ends
            while (true) {
                doReport = expandPE = true;
                skipWhitespace();
                doReport = expandPE = false;
                if (tryRead(']')) {
                    break; // end of subset
                } else {
                    // WFC, PEs in internal subset (only between decls)
                    peIsError = expandPE = true;
                    parseMarkupdecl();
                    peIsError = expandPE = false;
                }
            }
        }
        skipWhitespace();
        require('>');

        // Read the external subset, if any
        InputSource subset;

        if (ids.systemId == null) {
            subset = handler.getExternalSubset(rootName, handler.getSystemId());
        } else {
            subset = null;
        }
        if ((ids.systemId != null) || (subset != null)) {
            pushString(null, ">");

            // NOTE: [dtd] is so we say what SAX2 expects,
            // though it's misleading (subset, not entire dtd)
            if (ids.systemId != null) {
                pushURL(true, "[dtd]", ids, null, null, null, true);
            } else {
                handler.warn("modifying document by adding external subset");
                pushURL(true, "[dtd]", new ExternalIdentifiers(
                        subset.getPublicId(), subset.getSystemId(), null),
                        subset.getCharacterStream(), subset.getByteStream(),
                        subset.getEncoding(), false);
            }

            // Loop until we end up back at '>'
            while (true) {
                doReport = expandPE = true;
                skipWhitespace();
                doReport = expandPE = false;
                if (tryRead('>')) {
                    break;
                } else {
                    expandPE = true;
                    parseMarkupdecl();
                    expandPE = false;
                }
            }

            // the ">" string isn't popped yet
            if (inputStack.size() != 1) {
                fatal("external subset has unmatched '>'");
            }
        }

        // done dtd
        handler.endDoctype();
        expandPE = false;
        doReport = true;
    }
1056778
private RpcElement readRpc(String documentation) {
    RpcElement.Builder builder = RpcElement.builder()
        .name(readName())
        .documentation(documentation);

    if (readChar() != '(') throw unexpected("expected '('");
    DataType requestType = readDataType();
    if (!(requestType instanceof NamedType)) {
      throw unexpected("expected message but was " + requestType);
    }
    builder.requestType((NamedType) requestType);
    if (readChar() != ')') throw unexpected("expected ')'");

    if (!readWord().equals("returns")) throw unexpected("expected 'returns'");

    if (readChar() != '(') throw unexpected("expected '('");
    DataType responseType = readDataType();
    if (!(responseType instanceof NamedType)) {
      throw unexpected("expected message but was " + responseType);
    }
    builder.responseType((NamedType) responseType);
    if (readChar() != ')') throw unexpected("expected ')'");

    if (peekChar() == '{') {
      pos++;
      while (true) {
        String rpcDocumentation = readDocumentation();
        if (peekChar() == '}') {
          pos++;
          break;
        }
        Object declared = readDeclaration(rpcDocumentation, Context.RPC);
        if (declared instanceof OptionElement) {
          builder.addOption((OptionElement) declared);
        }
      }
    } else if (readChar() != ';') throw unexpected("expected ';'");

    return builder.build();
  }
1111155
def _parse_docblocks(self, string, container):
        
        #The easiest way to do this is to look at one line at a time and see if it is a docstring
        #When we find a group of docstrings that suddenly ends, the next item is the code element
        #that they were decorating (which may or may not be pertinent).
        from fortpy.utility import XML
        current = []
        docblocks = {}
        docstart = 0

        for line in string.split("\n"):
            match = self.RE_DOCS.match(line)
            if match is not None:
                current.append(match.group("docstring"))
                if len(current) == 1:
                    #This was the first docstring of a new documentation block.
                    docend = docstart + len(line) + 1  # +1 for \n removed by split()
                else:
                    #We already have some docstrings in the block, update start/end
                    docend += len(line) + 1
            else:
                #See if we were previously working on a docstring block or not.
                if len(current) > 0:
                    #Save all the current docstrings in the blocks dictionary
                    #under the name of the code element in this line.
                    key = self._parse_docline(line, container)
                    #If the docblock has multiple XML tags at the same depth, the XML
                    #parser will scream. Wrap everything in a doc tag.
                    doctext = "<doc>{}</doc>".format(" ".join(current))
                    try:
                        #Let the docstart and docend *always* be absolute character references.
                        tabsstart, tabsend = container.module.absolute_charindex(string, docstart, docend-len(line))
                        emsg="module '{0}' docstring starting @ {1[0]}:{1[1]}"
                        emsg=emsg.format(container.module.name, container.module.linenum(tabsstart))
                        docs = XML(doctext, emsg)
                        if not key in docblocks:
                            absstart, absend = tabsstart, tabsend
                            docblocks[key] = [list(docs), absstart, absend]
                        else:
                            docblocks[key][0].extend(list(docs))
                    except ET.ParseError as err:
                        msg.err(err.msg)

                    #Reset the list of current docstrings
                    current = []
                    docstart = docend + len(line) + 1
                else:
                    #We need to keep track of the line lengths for docstart/end.
                    docstart += len(line) + 1

        return docblocks
796387
def parsexmldeclarations(self, node):
        """"""
        if self.debug >= 1:
            print("[PyNLPl FoLiA DEBUG] Processing Annotation Declarations",file=stderr)
        self.declareprocessed = True
        for subnode in node: #pylint: disable=too-many-nested-blocks
            if not isinstance(subnode.tag, str): continue
            if subnode.tag[:25] == '{' + NSFOLIA + '}' and subnode.tag[-11:] == '-annotation':
                prefix = subnode.tag[25:][:-11]
                type = None
                if prefix.upper() in vars(AnnotationType):
                    type = vars(AnnotationType)[prefix.upper()]
                else:
                    raise Exception("Unknown declaration: " + subnode.tag)

                if 'set' in subnode.attrib and subnode.attrib['set']:
                    set = subnode.attrib['set']
                else:
                    set = 'undefined'

                if (type,set) in self.annotations:
                    if type == AnnotationType.TEXT:
                        #explicit Text declaration, remove the implicit declaration:
                        a = []
                        for t,s in self.annotations:
                            if not (t == AnnotationType.TEXT and s == 'undefined'):
                                a.append( (t,s) )
                        self.annotations = a
                    #raise ValueError("Double declaration of " + subnode.tag + ", set '" + set + "' + is already declared")    //doubles are okay says Ko
                else:
                    self.annotations.append( (type, set) )

                #Load set definition
                if set and self.loadsetdefinitions and set not in self.setdefinitions:
                    if set[:7] == "http://" or set[:8] == "https://" or set[:6] == "ftp://":
                        try:
                            self.setdefinitions[set] = SetDefinition(set,verbose=self.verbose) #will raise exception on error
                        except DeepValidationError:
                            print("WARNING: Set " + set + " could not be downloaded, ignoring!",file=sys.stderr) #warning and ignore

                #Set defaults
                if type in self.annotationdefaults and set in self.annotationdefaults[type]:
                    #handle duplicate. If ambiguous: remove defaults
                    if 'annotator' in subnode.attrib:
                        if not ('annotator' in self.annotationdefaults[type][set]):
                            self.annotationdefaults[type][set]['annotator'] = subnode.attrib['annotator']
                        elif self.annotationdefaults[type][set]['annotator'] != subnode.attrib['annotator']:
                            del self.annotationdefaults[type][set]['annotator']
                    if 'annotatortype' in subnode.attrib:
                        if not ('annotatortype' in self.annotationdefaults[type][set]):
                            self.annotationdefaults[type][set]['annotatortype'] = subnode.attrib['annotatortype']
                        elif self.annotationdefaults[type][set]['annotatortype'] != subnode.attrib['annotatortype']:
                            del self.annotationdefaults[type][set]['annotatortype']
                else:
                    defaults = {}
                    if 'annotator' in subnode.attrib:
                        defaults['annotator'] = subnode.attrib['annotator']
                    if 'annotatortype' in subnode.attrib:
                        if subnode.attrib['annotatortype'] == 'auto':
                            defaults['annotatortype'] = AnnotatorType.AUTO
                        else:
                            defaults['annotatortype'] = AnnotatorType.MANUAL
                    if 'datetime' in subnode.attrib:
                        if isinstance(subnode.attrib['datetime'], datetime):
                            defaults['datetime'] = subnode.attrib['datetime']
                        else:
                            defaults['datetime'] = parse_datetime(subnode.attrib['datetime'])

                    if not type in self.annotationdefaults:
                        self.annotationdefaults[type] = {}
                    self.annotationdefaults[type][set] = defaults


                if 'external' in subnode.attrib and subnode.attrib['external']:
                    if self.debug >= 1:
                        print("[PyNLPl FoLiA DEBUG] Loading external document: " + subnode.attrib['external'],file=stderr)
                    if not type in self.standoffdocs:
                        self.standoffdocs[type] = {}
                    self.standoffdocs[type][set] = {}

                    #check if it is already loaded, if multiple references are made to the same doc we reuse the instance
                    standoffdoc = None
                    for t in self.standoffdocs:
                        for s in self.standoffdocs[t]:
                            for source in self.standoffdocs[t][s]:
                                if source == subnode.attrib['external']:
                                    standoffdoc = self.standoffdocs[t][s]
                                    break
                            if standoffdoc: break
                        if standoffdoc: break

                    if not standoffdoc:
                        if subnode.attrib['external'][:7] == 'http://' or subnode.attrib['external'][:8] == 'https://':
                            #document is remote, download (in memory)
                            try:
                                f = urlopen(subnode.attrib['external'])
                            except:
                                raise DeepValidationError("Unable to download standoff document: " + subnode.attrib['external'])
                            try:
                                content = u(f.read())
                            except IOError:
                                raise DeepValidationError("Unable to download standoff document: " + subnode.attrib['external'])
                            f.close()
                            standoffdoc = Document(string=content, parentdoc=self, setdefinitions=self.setdefinitions)
                        elif os.path.exists(subnode.attrib['external']):
                            #document is on disk:
                            standoffdoc = Document(file=subnode.attrib['external'], parentdoc=self, setdefinitions=self.setdefinitions)
                        else:
                            #document not found
                            raise DeepValidationError("Unable to find standoff document: " + subnode.attrib['external'])

                    self.standoffdocs[type][set][subnode.attrib['external']] = standoffdoc
                    standoffdoc.parentdoc = self

                if self.debug >= 1:
                    print("[PyNLPl FoLiA DEBUG] Found declared annotation " + subnode.tag + ". Defaults: " + repr(defaults),file=stderr)
4019
def _doc_parse(doc, module=None, qualname=None):
    

    # Build the rex
    known_tags = {
        'param': 'arg',
        'type': 'arg-type',
        'return': 'ret',
        'returns': 'ret',
        'rtype': 'ret-type',
        'exception': 'exc',
        'except': 'exc',
        'raise': 'exc',
        'raises': 'exc',
    }
    tag_rex = re.compile(r'^\s*:(' + '|'.join(map(re.escape, known_tags)) + r')\s*(\S+)?\s*:', re.MULTILINE)

    # Match tags
    collect_args = {}
    collect_ret = {}
    doc_args = []
    doc_exc = []
    for m in reversed(list(tag_rex.finditer(doc))):
        # Fetch data
        tag, arg = m.groups()
        tag = known_tags[tag]  # Normalized tag name

        # Fetch docstring part
        value = doc[m.end():].strip()  # Copy text after the tag
        doc = doc[:m.start()].strip()  # truncate the string

        # Handle tag: collect data
        if tag == 'exc':
            doc_exc.append(data.ExceptionDoc(arg, value))
        elif tag in ('ret', 'ret-type'):
            # Collect fields 1 by 1
            collect_ret[{'ret': 'doc', 'ret-type': 'type'}[tag]] = value
        elif tag in ('arg', 'arg-type'):
            # Init new collection
            if arg not in collect_args:
                doc_args.append(arg)  # add name for now, then map() replace with classes
                collect_args[arg] = {}
            # Collect fields 1 by 1
            collect_args[arg][{'arg': 'doc', 'arg-type': 'type'}[tag]] = value
        else:
            raise AssertionError('Unknown tag type: {}'.format(tag))

    # Merge collected data
    doc_ret = data.ValueDoc(**collect_ret) if collect_ret else None
    doc_args = map(lambda name: data.ArgumentDoc(name=name, **collect_args[name]), collect_args)

    # Finish
    return data.FDocstring(module=module, qualname=qualname, doc=doc, args=doc_args, exc=doc_exc, ret=doc_ret)
1272714
private String parse(DocumentContext document) throws ParseException {
    if (document.namespaceDeclaration() != null) {
      setCurrentNamespace(
        document.namespaceDeclaration().qualifiedIdentifier().value);
    } else {
      setCurrentNamespace("");
    }

    setCurrentImports(document.importDeclarations());
    NamedDataSchema schema = parseNamedType(document.namedTypeDeclaration());
    topLevelSchemas.add(schema);
    return schema.getFullName();
  }
259795
private Decl parseImportDeclaration() {
		int start = index;
		EnclosingScope scope = new EnclosingScope();
		match(Import);
		Identifier fromName = parseOptionalFrom(scope);
		Tuple<Identifier >filterPath = parseFilterPath(scope);
		int end = index;
		matchEndLine();
		Decl.Import imprt;
		if(fromName != null) {
			imprt = new Decl.Import(filterPath, fromName);
		} else {
			imprt = new Decl.Import(filterPath);
		}
		return annotateSourceLocation(imprt, start);
	}
291986
def parse_object_type_definition(lexer: Lexer) -> Node:
    """"""
    start = lexer.token
    description = parse_description(lexer)
    expect_keyword(lexer, "type")
    name = parse_name(lexer)
    interfaces = parse_implements_interfaces(lexer)
    directives = parse_directives(lexer, True)
    fields = parse_fields_definition(lexer)
    return Node(
        description=description,
        name=name,
        interfaces=interfaces,
        directives=directives,
        fields=fields,
        loc=loc(lexer, start),
    )
644869
def parse(filename, return_doctype_dict=False):
    
    doctype_dict = {}
    # check for python version, doctype in ElementTree is deprecated 3.2 and above
    if sys.version_info < (3,2):
        parser = CustomXMLParser(html=0, target=None, encoding='utf-8')
    else:
        # Assume greater than Python 3.2, get the doctype from the TreeBuilder
        tree_builder = CustomTreeBuilder()
        parser = ElementTree.XMLParser(html=0, target=tree_builder, encoding='utf-8')

    tree = ElementTree.parse(filename, parser)
    root = tree.getroot()

    if sys.version_info < (3,2):
        doctype_dict = parser.doctype_dict
    else:
        doctype_dict = tree_builder.doctype_dict

    if return_doctype_dict is True:
        return root, doctype_dict
    else:
        return root
1314715
def _docstring_parse(self, blocks):
        """"""
        result = {}
        for block, docline, doclength, key in blocks:
            doctext = "<doc>{}</doc>".format(" ".join(block))
            try:
                docs = ET.XML(doctext)
                docstart = self.parser.charindex(docline, 0, self.context)
                if not key in result:
                    result[key] = [list(docs), docstart, docstart + doclength]
                else:
                    #If there are docblocks separated by whitespace in the
                    #same element we can't easily keep track of the start and
                    #end character indices anymore.
                    result[key][0].extend(list(docs))
            except ET.ParseError:
                msg.warn(doctext)

        return result
69443
private Document readDocument(byte[] xhtmlData) throws ParserConfigurationException, SAXException, IOException {

        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        docBuilderFactory.setValidating(false);
        docBuilderFactory.setNamespaceAware(true);
        DocumentBuilder docbuilder = docBuilderFactory.newDocumentBuilder();
        // use special entity resolver so we don't fetch the DTDs from w3.org, which would be slow
        docbuilder.setEntityResolver(m_entityResolver);
        Document doc = docbuilder.parse(new ByteArrayInputStream(xhtmlData));
        return doc;
    }
257580
def _parse_desc(node):
    
    desc = ''
    if len(node) == 0:
        return '<p>' + node.text + '</p>'
    for n in node:
        if n.tag == 'p':
            desc += '<p>' + _join_lines(n.text) + '</p>'
        elif n.tag == 'ol' or n.tag == 'ul':
            desc += '<ul>'
            for c in n:
                if c.tag == 'li':
                    desc += '<li>' + _join_lines(c.text) + '</li>'
                else:
                    raise ParseError('Expected <li> in <%s>, got <%s>' % (n.tag, c.tag))
            desc += '</ul>'
        else:
            raise ParseError('Expected <p>, <ul>, <ol> in <%s>, got <%s>' % (node.tag, n.tag))
    return desc
684721
function parseConstLetDeclaration(kind) {
        var declarations, startToken;

        startToken = lookahead;

        expectKeyword(kind);

        declarations = parseVariableDeclarationList(kind);

        consumeSemicolon();

        return delegate.markEnd(delegate.createVariableDeclaration(declarations, kind), startToken);
    }
219239
def parse(self, data, doctype):
        '''
        
        '''
        self.doctype = doctype
        self.lexer.lineno = 0
        del self.errors[:]
        del self.warnings[:]
        self.lexer.lexerror = False
        ast = self.parser.parse(data, lexer=self.lexer)
        if self.lexer.lexerror:
            ast = None
        if ast is None:
            self.errors.append("Couldn't build AST.")
        else:
            for check in self.sema[self.doctype]:
                visitor = check()
                if not visitor.visit(ast):
                    self.errors.append("Couldn't visit AST.")
                self.errors.extend(visitor.errors)
                self.warnings.extend(visitor.warnings)
        return (ast, list(self.errors), list(self.warnings))
1223556
private Node parseParamTypeExpressionAnnotation(JsDocToken token) {
    checkArgument(token == JsDocToken.LEFT_CURLY);

    skipEOLs();

    Node typeNode = parseParamTypeExpression(next());
    if (typeNode != null) {
      if (!match(JsDocToken.RIGHT_CURLY)) {
        reportTypeSyntaxWarning("msg.jsdoc.missing.rc");
      } else {
        next();
      }
    }

    return typeNode;
  }
889438
private void outputDocTypeDecl(String name) throws SAXException {
        if (true == m_needToOutputDocTypeDecl)
        {
            String doctypeSystem = getDoctypeSystem();
            String doctypePublic = getDoctypePublic();
            if ((null != doctypeSystem) || (null != doctypePublic))
            {
                final java.io.Writer writer = m_writer;
                try
                {
                writer.write("<!DOCTYPE ");
                writer.write(name);

                if (null != doctypePublic)
                {
                    writer.write(" PUBLIC \"");
                    writer.write(doctypePublic);
                    writer.write('"');
                }

                if (null != doctypeSystem)
                {
                    if (null == doctypePublic)
                        writer.write(" SYSTEM \"");
                    else
                        writer.write(" \"");

                    writer.write(doctypeSystem);
                    writer.write('"');
                }

                writer.write('>');
                outputLineSep();
                }
                catch(IOException e)
                {
                    throw new SAXException(e);
                }
            }
        }

        m_needToOutputDocTypeDecl = false;
    }
1244083
def parse_declaration(self, tup_tree):
        

        self.check_node(tup_tree, 'DECLARATION')

        child = self.one_child(tup_tree, ('DECLGROUP',))

        return name(tup_tree), attrs(tup_tree), child