/****************************************************************
 * Licensed to the Apache Software Foundation (ASF) under one   *
 * or more contributor license agreements.  See the NOTICE file *
 * distributed with this work for additional information        *
 * regarding copyright ownership.  The ASF licenses this file   *
 * to you under the Apache License, Version 2.0 (the            *
 * "License"); you may not use this file except in compliance   *
 * with the License.  You may obtain a copy of the License at   *
 *                                                              *
 *   http://www.apache.org/licenses/LICENSE-2.0                 *
 *                                                              *
 * Unless required by applicable law or agreed to in writing,   *
 * software distributed under the License is distributed on an  *
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
 * KIND, either express or implied.  See the License for the    *
 * specific language governing permissions and limitations      *
 * under the License.                                           *
 ****************************************************************/

options {
  static=false;
  JDK_VERSION = "1.5";
  OUTPUT_DIRECTORY = "../../../../../../../../../target/generated-sources/javacc";
}

PARSER_BEGIN(ContentLanguageParser)
/****************************************************************
 * Licensed to the Apache Software Foundation (ASF) under one   *
 * or more contributor license agreements.  See the NOTICE file *
 * distributed with this work for additional information        *
 * regarding copyright ownership.  The ASF licenses this file   *
 * to you under the Apache License, Version 2.0 (the            *
 * "License"); you may not use this file except in compliance   *
 * with the License.  You may obtain a copy of the License at   *
 *                                                              *
 *   http://www.apache.org/licenses/LICENSE-2.0                 *
 *                                                              *
 * Unless required by applicable law or agreed to in writing,   *
 * software distributed under the License is distributed on an  *
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
 * KIND, either express or implied.  See the License for the    *
 * specific language governing permissions and limitations      *
 * under the License.                                           *
 ****************************************************************/
package org.apache.james.mime4j.field.language.parser;

import java.util.ArrayList;
import java.util.List;

public class ContentLanguageParser {
	private List<String> languages = new ArrayList<String>();
	
	/**
	 * Parses the input into a list of language tags.
	 * @return list of language tag Strings
	 */
	public List<String> parse() throws ParseException {
		try {
			return doParse();
		} catch (TokenMgrError e) {
			// An issue with the TOKENiser 
			// but it's not polite to throw an Error
			// when executing on a server
			throw new ParseException(e);
		}
	}
}
PARSER_END(ContentLanguageParser)

private List<String> doParse() :
{
}
{
	language() ( "," language() )*
	{return languages;}
}

String language() :
{
	Token token;
	StringBuffer languageTag = new StringBuffer();
	String result;
}
{
	token = <ALPHA> 
			{
				languageTag.append(token.image);
			}
		( 
		"-" 
// This keeps TOKENising simple
				token = <ALPHA> 
					{
						languageTag.append('-');
						languageTag.append(token.image);
					}
				|
				token = <ALPHANUM> 
					{
						languageTag.append('-');
						languageTag.append(token.image);
					}
		)*
	
	{
		result = languageTag.toString();
		languages.add(result);
		return result;
	}
}




SPECIAL_TOKEN :
{
 	< WS: ( [" ", "\t", "\r", "\n"] )+ >
}

TOKEN_MGR_DECLS :
{
	// Keeps track of how many levels of comment nesting
	// we've encountered.  This is only used when the 2nd
	// level is reached, for example ((this)), not (this).
	// This is because the outermost level must be treated
	// specially anyway, because the outermost ")" has a
	// different token type than inner ")" instances.
	int commentNest;
}


MORE :
{
	// starts a comment
	"(" : INCOMMENT
}

<INCOMMENT>
SKIP :
{
	// ends a comment
	< COMMENT: ")" > : DEFAULT
	// if this is ever changed to not be a SKIP, need
	// to make sure matchedToken.token = token.toString()
	// is called.
}

<INCOMMENT>
MORE :
{
	< <QUOTEDPAIR>> { image.deleteCharAt(image.length() - 2); }
|	"(" { commentNest = 1; } : NESTED_COMMENT
|	< <ANY>>
}

<NESTED_COMMENT>
MORE :
{
	< <QUOTEDPAIR>> { image.deleteCharAt(image.length() - 2); }
|	"(" { ++commentNest; }
|	")" { --commentNest; if (commentNest == 0) SwitchTo(INCOMMENT); }
|	< <ANY>>
}
// QUOTED STRINGS

MORE :
{
	"\"" { image.deleteCharAt(image.length() - 1); } : INQUOTEDSTRING
}

<INQUOTEDSTRING>
MORE :
{
	< <QUOTEDPAIR>> { image.deleteCharAt(image.length() - 2); }
|	< (~["\"", "\\"])+ >
}

<INQUOTEDSTRING>
TOKEN :
{
	< QUOTEDSTRING: "\"" > { matchedToken.image = image.substring(0, image.length() - 1); } : DEFAULT
}

TOKEN :
{
	< DIGITS: ( ["0"-"9"] )+ >
}

TOKEN :
{
	< ALPHA: ( ["a"-"z"] | ["A"-"Z"] )+ >
}

TOKEN :
{
	<ALPHANUM : (  ["0"-"9"] |  ["a"-"z"] | ["A"-"Z"] )+>
}

TOKEN :
{
	< DOT: "." >
}

<*>
TOKEN :
{
	< #QUOTEDPAIR: "\\" <ANY> >
|	< #ANY: ~[] >
}