Skip to content

Commit

Permalink
Fix for issue #2
Browse files Browse the repository at this point in the history
  • Loading branch information
hsolbrig committed Apr 10, 2018
1 parent 63a9023 commit e5b491d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
5 changes: 3 additions & 2 deletions parsers/python/pyshexc/parser_impl/generate_shexj.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,11 @@ def do_parse(infilename: str, jsonfilename: Optional[str], rdffilename: Optional
return False


def parse(input_: Union[str, FileStream]) -> Optional[Schema]:
def parse(input_: Union[str, FileStream], default_base: Optional[str]=None) -> Optional[Schema]:
"""
Parse the text in infile and return the resulting schema
:param input_: text or input stream to parse
:param default_base_: base URI for relative URI's in schema
:return: ShExJ Schema object. None if error.
"""

Expand All @@ -133,7 +134,7 @@ def parse(input_: Union[str, FileStream]) -> Optional[Schema]:
return None

# Step 3: Transform the results the results
parser = ShexDocParser()
parser = ShexDocParser(default_base=default_base)
parser.visit(parse_tree)

return parser.context.schema
Expand Down
11 changes: 7 additions & 4 deletions parsers/python/pyshexc/parser_impl/shex_doc_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,31 @@
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
# OF THE POSSIBILITY OF SUCH DAMAGE.
from typing import Optional

from pyshexc.parser.ShExDocParser import ShExDocParser
from pyshexc.parser.ShExDocVisitor import ShExDocVisitor

from pyshexc.parser_impl.parser_context import ParserContext
from pyshexc.parser_impl.shex_annotations_and_semacts_parser import ShexAnnotationAndSemactsParser
from pyshexc.parser_impl.shex_shape_expression_parser import ShexShapeExpressionParser
from ShExJSG.ShExJ import ShapeExternal
from ShExJSG.ShExJ import ShapeExternal, IRIREF


class ShexDocParser(ShExDocVisitor):
""" parser for sheExDoc production """
def __init__(self):
def __init__(self, default_base: Optional[str]=None):
ShExDocVisitor.__init__(self)
self.context = ParserContext() # ParserContext
self.context = ParserContext()
self.context.base = IRIREF(default_base) if default_base else None

def visitShExDoc(self, ctx: ShExDocParser.ShExDocContext):
""" shExDoc: directive* ((notStartAction | startActions) statement*)? EOF """
self.context = ParserContext()
self.visitChildren(ctx)

def visitBaseDecl(self, ctx: ShExDocParser.BaseDeclContext):
""" baseDecl: KW_BASE IRIREF """
self.context.base = None
self.context.base = self.context.iriref_to_shexj_iriref(ctx.IRIREF())

def visitPrefixDecl(self, ctx: ShExDocParser.PrefixDeclContext):
Expand Down
41 changes: 38 additions & 3 deletions parsers/python/tests/test_issue_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,52 @@
# OF THE POSSIBILITY OF SUCH DAMAGE.

import unittest
from rdflib import Namespace

from ShExJSG import ShExJ

from pyshexc.parser_impl.generate_shexj import parse

BASE = Namespace("https://raw.githubusercontent.com/shexSpec/shexTest/master/validation/")
FOO = Namespace("/some/location/file/")
EX = Namespace("http://example.org/")


class TestIssue2(unittest.TestCase):
def test_something(self):
def test_no_base(self):
shex_str = '<S1> {<p1> [<o1>]}'
shex: ShExJ.Schema = parse(shex_str)
self.assertEqual("S1", str(shex.shapes[0].id))
self.assertEqual("p1", str(shex.shapes[0].expression.predicate))
self.assertEqual("o1", str(shex.shapes[0].expression.valueExpr.values[0]))

def test_default_base(self):
shex_str = '<S1> {<p1> [<o1>]}'
shex = parse(shex_str)
self.assertTrue(isinstance(shex, ShExJ.Schema))
shex: ShExJ.Schema = parse(shex_str, str(BASE))
self.assertEqual(str(BASE.S1),
str(shex.shapes[0].id))
self.assertEqual(str(BASE.p1), str(shex.shapes[0].expression.predicate))
self.assertEqual(str(BASE.o1), str(shex.shapes[0].expression.valueExpr.values[0]))

def test_explicit_base(self):
shex_str = f'BASE <{str(FOO)}>\n<S1> {{<p1> [<o1>]}}'
shex: ShExJ.Schema = parse(shex_str, str(BASE))
self.assertEqual(str(FOO.S1),
str(shex.shapes[0].id))
self.assertEqual(str(FOO.p1), str(shex.shapes[0].expression.predicate))
self.assertEqual(str(FOO.o1), str(shex.shapes[0].expression.valueExpr.values[0]))

def test_explicit_uris(self):
shex_str = f"""
BASE <{str(FOO)}>
PREFIX ex: <{EX}>
ex:S1 {{ex:p1 [ex:o1]}}"""
shex: ShExJ.Schema = parse(shex_str, str(BASE))
self.assertEqual(str(EX.S1),
str(shex.shapes[0].id))
self.assertEqual(str(EX.p1), str(shex.shapes[0].expression.predicate))
self.assertEqual(str(EX.o1), str(shex.shapes[0].expression.valueExpr.values[0]))


if __name__ == '__main__':
Expand Down

0 comments on commit e5b491d

Please sign in to comment.