private INode TryParseLiteral(TokenisingResultParserContext context, IToken t) { String value; if (t.TokenType == Token.LITERAL) { value = t.Value; } else if (t.TokenType == Token.LONGLITERAL) { value = t.Value; } else { value = t.Value; if (value.Equals("true") || value.Equals("false")) { return(context.Handler.CreateLiteralNode(value, UriFactory.Create(XmlSpecsHelper.XmlSchemaDataTypeBoolean))); } else { Uri plUri = TurtleSpecsHelper.InferPlainLiteralType((PlainLiteralToken)t, TurtleSyntax.Original); return(context.Handler.CreateLiteralNode(value, plUri)); } } // Check for DataType/Language Specifier IToken next = context.Tokens.Peek(); if (next.TokenType == Token.DATATYPE) { next = context.Tokens.Dequeue(); Uri dtUri = UriFactory.Create(next.Value.Substring(1, next.Length - 2)); return(context.Handler.CreateLiteralNode(value, dtUri)); } else if (next.TokenType == Token.LANGSPEC) { next = context.Tokens.Dequeue(); return(context.Handler.CreateLiteralNode(value, next.Value)); } else { return(context.Handler.CreateLiteralNode(value)); } }
private void TryParseCollection(TriGParserContext context, Uri graphUri, INode subj) { //Create the Nodes we need IUriNode rdfFirst, rdfRest, rdfNil; rdfFirst = context.Handler.CreateUriNode(UriFactory.Create(RdfSpecsHelper.RdfListFirst)); rdfRest = context.Handler.CreateUriNode(UriFactory.Create(RdfSpecsHelper.RdfListRest)); rdfNil = context.Handler.CreateUriNode(UriFactory.Create(RdfSpecsHelper.RdfListNil)); IToken next; INode item, temp; item = null; do { next = context.Tokens.Dequeue(); //Create a Node for this Token switch (next.TokenType) { case Token.COMMENT: //Discard and continue; continue; case Token.QNAME: case Token.URI: item = ParserHelper.TryResolveUri(context, next); break; case Token.LITERAL: case Token.LONGLITERAL: break; case Token.PLAINLITERAL: Uri plt = TurtleSpecsHelper.InferPlainLiteralType((PlainLiteralToken)next, TurtleSyntax.Original); item = context.Handler.CreateLiteralNode(next.Value, plt); break; case Token.LEFTSQBRACKET: //Check whether an anonymous Blank Node or a Blank Node Collection item = context.Handler.CreateBlankNode(); next = context.Tokens.Peek(); if (next.TokenType == Token.RIGHTSQBRACKET) { //Anonymous Blank Node context.Tokens.Dequeue(); } else { //Blank Node Collection this.TryParsePredicateObjectList(context, graphUri, item); } break; case Token.LEFTBRACKET: //Check whether an Empty Collection next = context.Tokens.Peek(); if (next.TokenType == Token.RIGHTBRACKET) { //Empty Collection context.Tokens.Dequeue(); item = context.Handler.CreateUriNode(UriFactory.Create(RdfSpecsHelper.RdfListNil)); } else { //Collection item = context.Handler.CreateBlankNode(); this.TryParseCollection(context, graphUri, item); } break; case Token.EOF: throw ParserHelper.Error("Unexpected End of File while trying to parse a Collection", next); default: //Unexpected Token throw ParserHelper.Error("Unexpected Token '" + next.GetType().ToString() + "' encountered, expected a URI/QName/Literal/Blank Node as an item in a Collection", next); } //Create the subj rdf:first item Triple if (!context.Handler.HandleTriple((new Triple(subj, rdfFirst, item, graphUri)))) { ParserHelper.Stop(); } //Create the rdf:rest Triple if (context.Tokens.Peek().TokenType == Token.RIGHTBRACKET) { //End of Collection context.Tokens.Dequeue(); if (!context.Handler.HandleTriple(new Triple(subj, rdfRest, rdfNil, graphUri))) { ParserHelper.Stop(); } return; } else { //Continuing Collection temp = context.Handler.CreateBlankNode(); if (!context.Handler.HandleTriple(new Triple(subj, rdfRest, temp, graphUri))) { ParserHelper.Stop(); } subj = temp; } } while (true); }
private void TryParseObjectList(TriGParserContext context, Uri graphUri, INode subj, INode pred) { bool ok = false; do { //After the first run through we'll need to discard commas here if (ok) { context.Tokens.Dequeue(); } //Try to get the Object IToken obj = context.Tokens.Dequeue(); IToken next; INode objNode; switch (obj.TokenType) { case Token.COMMENT: //Discard and Continue ok = false; continue; case Token.QNAME: case Token.URI: objNode = ParserHelper.TryResolveUri(context, obj); break; case Token.LITERAL: case Token.LONGLITERAL: //Literals //See whether we get a Language Specifier/Data Type next next = context.Tokens.Peek(); if (next.TokenType == Token.LANGSPEC) { //Literal with Language Specifier context.Tokens.Dequeue(); objNode = context.Handler.CreateLiteralNode(obj.Value, next.Value); } else if (next.TokenType == Token.HATHAT) { //Literal with DataType context.Tokens.Dequeue(); //Now expect a QName/Uri Token next = context.Tokens.Dequeue(); if (next.TokenType == Token.QNAME || next.TokenType == Token.URI) { Uri dt = UriFactory.Create(Tools.ResolveUriOrQName(next, context.Namespaces, context.BaseUri)); objNode = context.Handler.CreateLiteralNode(obj.Value, dt); } else { throw ParserHelper.Error("Unexpected Token '" + next.GetType().ToString() + "' encountered, expected a URI/QName Token to specify a Data Type after a ^^ Token", next); } } else { //Just a string literal objNode = context.Handler.CreateLiteralNode(obj.Value); } break; case Token.PLAINLITERAL: //Plain Literals Uri plt = TurtleSpecsHelper.InferPlainLiteralType((PlainLiteralToken)obj, TurtleSyntax.Original); objNode = context.Handler.CreateLiteralNode(obj.Value, plt); break; case Token.BLANKNODEWITHID: //Blank Node with ID objNode = context.Handler.CreateBlankNode(obj.Value.Substring(2)); break; case Token.LEFTSQBRACKET: //Blank Node next = context.Tokens.Peek(); if (next.TokenType == Token.RIGHTSQBRACKET) { //Anonymous Blank Node context.Tokens.Dequeue(); objNode = context.Handler.CreateBlankNode(); } else { //Blank Node Collection objNode = context.Handler.CreateBlankNode(); //Do an extra call to TryParsePredicateObjectList to parse the Blank Node Collection this.TryParsePredicateObjectList(context, graphUri, objNode); } break; case Token.RIGHTSQBRACKET: //End of Blank Node Collection if (!ok) { throw ParserHelper.Error("Unexpected Token '" + obj.GetType().ToString() + "' encountered before an Object list of a Blank Node Collection was parsed", obj); } return; case Token.LEFTBRACKET: //Collection //Check whether an Empty Collection next = context.Tokens.Peek(); if (next.TokenType == Token.RIGHTBRACKET) { //Empty Collection context.Tokens.Dequeue(); objNode = context.Handler.CreateUriNode(UriFactory.Create(RdfSpecsHelper.RdfListNil)); } else { //Collection objNode = context.Handler.CreateBlankNode(); this.TryParseCollection(context, graphUri, objNode); } break; case Token.EOF: throw ParserHelper.Error("Unexpected End of File while trying to parse Object List", obj); case Token.DOT: case Token.RIGHTCURLYBRACKET: case Token.SEMICOLON: if (!ok) { throw ParserHelper.Error("Unexpected Token '" + obj.GetType().ToString() + "' encountered before an Object list was parsed", obj); } return; default: //Unexpected Token throw ParserHelper.Error("Unexpected Token '" + obj.GetType().ToString() + "' encountered, expected a URI/QName/Blank Node as the Object of a Triple", obj); } ok = true; if (!context.Handler.HandleTriple(new Triple(subj, pred, objNode, graphUri))) { ParserHelper.Stop(); } } while (context.Tokens.Peek().TokenType == Token.COMMA); //Expect a comma if we are to continue }