diff options
author | Timotej Lazar <timotej.lazar@fri.uni-lj.si> | 2016-05-16 09:07:58 +0200 |
---|---|---|
committer | Timotej Lazar <timotej.lazar@fri.uni-lj.si> | 2016-05-16 09:07:58 +0200 |
commit | 5266ae684cce210de6521644aed0bf6f0fbf1e31 (patch) | |
tree | f53750b60bd69a0e054ad41c98e4f5ddf354baba | |
parent | 13cc62c244a35fefe3c6995ad16a09275b984f8d (diff) |
Detect predicates used in DCG clauses
-rw-r--r-- | prolog/util.py | 13 |
1 files 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) |