From 455369b4d18d0298ce406616246d346d5d22fa80 Mon Sep 17 00:00:00 2001 From: Christoph Kindl Date: Sat, 22 Aug 2015 13:02:11 +0200 Subject: Further fixes and improvements (pointed out by dscorbett). * fixed operator rule * fixed decimal/double rule * added bnode name rule * simplified IRI name rule * extended SPARQL example to cover more cases --- pygments/lexers/rdf.py | 15 +++++++++------ tests/examplefiles/sparql.rq | 27 ++++++++++++++++++++++----- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/pygments/lexers/rdf.py b/pygments/lexers/rdf.py index d25dcd82..b7e20801 100644 --- a/pygments/lexers/rdf.py +++ b/pygments/lexers/rdf.py @@ -20,7 +20,7 @@ __all__ = ['SparqlLexer'] class SparqlLexer(RegexLexer): """ - Lexer for `SPARQL `_ query language. + Lexer for `SPARQL ` query language. .. versionadded:: 2.0 """ @@ -30,6 +30,8 @@ class SparqlLexer(RegexLexer): mimetypes = ['application/sparql-query'] flags = re.IGNORECASE + + EXPONENT = r'e[+-]?\d+' tokens = { 'root': [ @@ -39,12 +41,13 @@ class SparqlLexer(RegexLexer): r'offset|bindings|load|clear|drop|create|add|move|copy|' r'insert\s+data|delete\s+data|delete\s+where|delete|insert|' r'using named|using|graph|default|named|all|optional|service|' - r'silent|bind|union|not in|in|as|a)', Keyword), + r'silent|bind|union|not in|in|as|a)\b', Keyword), (r'(prefix|base)(\s+)([a-z][\w-]*)(\s*)(\:)', bygroups(Keyword, Whitespace, Name.Namespace, Whitespace, Punctuation)), (r'\?[a-z_]\w*', Name.Variable), - (r'<([^<>"{}|^`\x5b-\x5d\x00-\x20])*>', Name.Label), + (r'<([^<>\s])*>', Name.Label), + (r'_:([^\s]+)', Name.Label), # simplified bnode name rule (r'([a-z][\w-]*)(\:)([a-z][\w-]*)', bygroups(Name.Namespace, Punctuation, Name.Tag)), (r'(str|lang|langmatches|datatype|bound|iri|uri|bnode|rand|abs|' @@ -56,10 +59,10 @@ class SparqlLexer(RegexLexer): r'count|sum|min|max|avg|sample|group_concat|separator)\b', Name.Function), (r'(true|false)', Literal), + (r'[+\-]?(\d+\.\d*e[+-]?\d+|\.?\d+e[+-]?\d+)', Number.Float), (r'[+\-]?\d*\.\d+', Number.Float), - (r'[+\-]?\d*(:?\.\d+)?E[+\-]?\d+', Number.Float), (r'[+\-]?\d+', Number.Integer), - (r'(\|\||&&|=|\*|\-|\+|/|!|<|>|<=|>=|!=)', Operator), + (r'(\|\||&&|=|\*|\-|\+|/|!=|<=|>=|!|<|>)', Operator), (r'[(){}.;,:^\[\]]', Punctuation), (r'#[^\n]+', Comment), (r'"""', String, 'triple-double-quoted-string'), @@ -91,7 +94,7 @@ class SparqlLexer(RegexLexer): (r'.', String, '#pop'), ], 'end-of-string': [ - (r'(@)([a-z]+(:?-[a-z0-9]+)*)', + (r'(@)([a-z]+(?:-[a-z0-9]+)*)', bygroups(Operator, Name.Function), '#pop:2'), (r'\^\^', Operator, '#pop:2'), default('#pop:2'), diff --git a/tests/examplefiles/sparql.rq b/tests/examplefiles/sparql.rq index caedfd14..98f67a93 100644 --- a/tests/examplefiles/sparql.rq +++ b/tests/examplefiles/sparql.rq @@ -6,18 +6,35 @@ PREFIX xsd: PREFIX dcterms: SELECT ?person (COUNT(?nick) AS ?nickCount) { + ?person foaf:firstName "Freddy" . + ?person foaf:lastName "Smith" . + # predicate-object list ?person foaf:nick ?nick ; - foaf:lastName "Smith" ; - foaf:age "21"^^xsd:int ; + foaf:age "21"^^xsd:int ; # typed literal ex:title 'Mr' ; # single-quoted string + ex:width 2 ; # integer ex:height 1.80 ; # float - ex:distanceToSun +1.4e8 ; # float with exponent + ex:distanceToSun 1.4e8 ; # float with exponent ex:ownsACat true ; - dcterms:description "Someone with a cat called \"cat\"."@en . + # some other float values + ex:float1 .125 ; + ex:float2 +2.5e10 ; + ex:float3 2.5e+10 ; + ex:float4 -1.e-10 ; + ex:float5 .0e1 ; + ex:float6 5e11 ; + ex:catName "Kitty", "Kitty_" ; # object list + ex:catAge ?catage ; + dcterms:description "Someone with a cat called \"cat\"."@en . # language tag + ?person foaf:knows _:b0 . + _:b0 foaf:knows [ _:b1 a foaf:Person; foaf:name "Jonny" . ] . OPTIONAL { ?person foaf:isPrimaryTopicOf ?page } OPTIONAL { ?person foaf:name ?name { ?person foaf:depiction ?img } UNION { ?person foaf:firstName ?firstN } } FILTER ( bound(?page) || bound(?img) || bound(?firstN) ) -} GROUP BY ?person ORDER BY ?img + FILTER ( ?catage < 101 && ?catage > 9 && ?catage >= 10 && ?catage <= 100 && ?catage != 20 ) +} +GROUP BY ?person +ORDER BY ?img ASC(?firstN) DESC(?page) -- cgit v1.2.1