/* 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). 
 */

/* This file is part of Xerophily, a parser for XSD regular expressions.
 *
 * Xerophily is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser Public License as published
 * by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 *
 * This program is distributed 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.  See the GNU
 * Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser Public License
 * along with this program.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

/* 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,[]).



