/* recognize.pl:  accept strings or atoms and run regex parser on them.
 */

/* Copyright (c) 2008 World Wide Web Consortium, 
 * (Massachusetts Institute of Technology, European Research 
 * Consortium for Informatics and Mathematics, Keio University). 
 * All Rights Reserved. This work is distributed under the 
 * W3C(TM) Software License [1] in the hope that it will be 
 * useful, but WITHOUT ANY WARRANTY; without even the implied 
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * [1] http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231
 */

/* This file is part of Xerophily, a parser for XSD regular expressions. */

/* Revisions:
 * 2008-03-28 : CMSMcQ : made file 
 */

:- module(parseregex,[
		     regex/2,
		     regex/3
		    ]).

:- ensure_loaded('regex.dcg.pl').
:- use_module(g_opts).

/* regex(+String,-AST):  parse string with regex grammar, return AST 
 * Use the default grammar, i.e. 'D8'
 */

regex(S,AST) :-
	default_grammar(G),
	regex(S,G,AST).

/* If we get the grammar, we must still figure out which
 * form the input is given in: SWI string, atom, or
 * old fashioned list-of-integers string
 */ 
regex(S,G,AST) :-
	string(S),
	string_to_atom(S,A),
	atom_chars(A,L),
	regex2(L,G,AST).
regex(A,G,AST) :-
	atom(A),
	atom_chars(A,L),
	regex2(L,G,AST).
regex(S,G,AST) :-
	is_list(S),
	atom_codes(A,S),
	atom_chars(A,L),
	regex2(L,G,AST).

grammar('1E').
grammar('PER').
grammar('2E').
grammar('D6').
grammar('LC1').
grammar('D8').
grammar('W').

default_grammar('D8').

regex2(List,G,AST) :-
	get_grammar_options(G,Opts),
	regExp(Opts,AST,List,[]).



