From 5266ae684cce210de6521644aed0bf6f0fbf1e31 Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Mon, 16 May 2016 09:07:58 +0200 Subject: Detect predicates used in DCG clauses --- prolog/util.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/prolog/util.py b/prolog/util.py index 83f891f..e1ce36b 100644 --- a/prolog/util.py +++ b/prolog/util.py @@ -196,8 +196,12 @@ def map_vars(a, b, tokens, variables): # Return a set of predicate names (e.g. conc/3) used in [program]. def used_predicates(program): predicates = set() - def walk(tree): + def walk(tree, dcg=False): if isinstance(tree, Tree): + # DCG predicates can be called without parameters + if tree.label() == 'clause' and len(tree) == 4 and \ + tree[1].type == 'FROMDCG': + dcg = True if tree.label() == 'term' and len(tree) >= 3 and \ isinstance(tree[0], Tree) and tree[0].label() == 'functor': if len(tree) == 3: @@ -205,7 +209,12 @@ def used_predicates(program): else: predicates.add('{}/{}'.format(tree[0][0], (len(tree[2])+1)//2)) for subtree in tree: - walk(subtree) + walk(subtree, dcg) + elif isinstance(tree, Token): + if dcg and tree.type == 'NAME': + predicates.add('{}/{}'.format(tree.val, 2)) + predicates.add('{}/{}'.format(tree.val, 3)) + predicates.add('{}/{}'.format(tree.val, 4)) tree = parse(program) if tree is not None: walk(tree) -- cgit v1.2.1