/*
 [The "BSD licence"]
 Copyright (c) 2005-2008 Terence Parr
 All rights reserved.

 Grammar conversion to ANTLR v3 and C#:
 Copyright (c) 2008 Sam Harwell, Pixel Mine, Inc.
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:
 1. Redistributions of source code must retain the above copyright
	notice, this list of conditions and the following disclaimer.
 2. Redistributions in binary form must reproduce the above copyright
	notice, this list of conditions and the following disclaimer in the
	documentation and/or other materials provided with the distribution.
 3. The name of the author may not be used to endorse or promote products
	derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
grammar Template;

options
{
	language=CSharp3;
}

@lexer::namespace {Antlr3.ST.Language}
@parser::namespace {Antlr3.ST.Language}

@lexer::ctorModifier{private}

public
template[StringTemplate self]
@init
{
	this.self = self;
}
	:	(	s=LITERAL  {self.AddChunk(new StringRef(self,$s.text));}
		|	nl=NEWLINE
			{
				int next = input.LA(1);
				if ( next!=ELSE && next!=ENDIF )
				{
					self.AddChunk(new NewlineRef(self,$nl.text));
				}
			}
		|	action[self]
		)*
		EOF?
	;

action[StringTemplate self]
	:	a=ACTION
		{
			string indent = ((ChunkToken)a).Indentation;
			ASTExpr c = self.ParseAction($a.text);
			c.Indentation = indent;
			self.AddChunk(c);
		}

	|	i=IF
		{
			ConditionalExpr c = (ConditionalExpr)self.ParseAction($i.text);
			// create and precompile the subtemplate
			StringTemplate subtemplate = new StringTemplate(self.Group, null);
			subtemplate.EnclosingInstance = self;
			subtemplate.Name = $i.text + "_subtemplate";
			self.AddChunk(c);
		}

		template[subtemplate] {if ( c!=null ) c.Subtemplate = subtemplate;}

		(	ei=ELSEIF
			{
				ASTExpr ec = self.ParseAction($ei.text);
				// create and precompile the subtemplate
				StringTemplate elseIfSubtemplate = new StringTemplate(self.Group, null);
				elseIfSubtemplate.EnclosingInstance = self;
				elseIfSubtemplate.Name = $ei.text + "_subtemplate";
			}

			template[elseIfSubtemplate]

			{if ( c!=null ) c.AddElseIfSubtemplate(ec, elseIfSubtemplate);}
		)*

		(	ELSE
			{
				// create and precompile the subtemplate
				StringTemplate elseSubtemplate = new StringTemplate(self.Group, null);
				elseSubtemplate.EnclosingInstance = self;
				elseSubtemplate.Name = "else_subtemplate";
			}

			template[elseSubtemplate]
			{if ( c!=null ) c.ElseSubtemplate = elseSubtemplate;}
		)?

		ENDIF

	|	rr=REGION_REF
		{
			// define implicit template and
			// convert <@r()> to <region__enclosingTemplate__r()>
			string regionName = $rr.text;
			string mangledRef = null;
			bool err = false;
			// watch out for <@super.r()>; that does NOT def implicit region
			// convert to <super.region__enclosingTemplate__r()>
			string regionNamePrefix = "super.";
			if ( regionName.StartsWith(regionNamePrefix) )
			{
				//System.Console.Out.WriteLine( "super region ref " + regionName );
				string regionRef = regionName.Substring(regionNamePrefix.Length, regionName.Length - regionNamePrefix.Length);
				string templateScope = self.Group.GetUnMangledTemplateName(self.Name);
				StringTemplate scopeST = self.Group.LookupTemplate(templateScope);
				if ( scopeST==null )
				{
					self.Group.Error("reference to region within undefined template: "+templateScope);
					err=true;
				}
				if ( !scopeST.ContainsRegionName(regionRef) )
				{
					self.Group.Error("template "+templateScope+" has no region called "+regionRef);
					err=true;
				}
				else
				{
					mangledRef = self.Group.GetMangledRegionName(templateScope,regionRef);
					mangledRef = "super."+mangledRef;
				}
			}
			else
			{
				//System.out.println("region ref "+regionName);
				StringTemplate regionST = self.Group.DefineImplicitRegionTemplate(self,regionName);
				mangledRef = regionST.Name;
			}

			if ( !err )
			{
				// treat as regular action: mangled template include
				string indent = ((ChunkToken)$rr).Indentation;
				ASTExpr c = self.ParseAction(mangledRef+"()");
				c.Indentation = indent;
				self.AddChunk(c);
			}
		}

	|	rd=REGION_DEF
		{
			string combinedNameTemplateStr = $rd.text;
			int indexOfDefSymbol = combinedNameTemplateStr.IndexOf("::=");
			if ( indexOfDefSymbol>=1 )
			{
				string regionName = combinedNameTemplateStr.Substring(0, indexOfDefSymbol);
				int startIndex = indexOfDefSymbol + 3;
				string template = combinedNameTemplateStr.Substring(startIndex, combinedNameTemplateStr.Length - startIndex);
				StringTemplate regionST = self.Group.DefineRegionTemplate(self,regionName,template,RegionType.Embedded);
				// treat as regular action: mangled template include
				string indent = ((ChunkToken)$rd).Indentation;
				ASTExpr c = self.ParseAction(regionST.Name+"()");
				c.Indentation = indent;
				self.AddChunk(c);
			}
			else
			{
				self.Error("embedded region definition screwed up");
			}
		}
	;

//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
// LEXER

// these rules are for manually-set tokens
fragment IF : ;
fragment ELSEIF : ;
fragment ELSE : ;
fragment ENDIF : ;
fragment REGION_DEF : ;
fragment REGION_REF : ;

NEWLINE
	:	('\r')? '\n' {currentIndent=null;}
	;

ACTION
@init
{
	int startCol = CharPositionInLine;
	System.Text.StringBuilder builder = new System.Text.StringBuilder();
	System.Text.StringBuilder buf = null;
	string subtext = string.Empty;
	char uc = '\0';
	bool atLeft = false;
	string t = null;
}
	:	(	options { k = 1; } :
			(LINE_BREAK) => LINE_BREAK {$channel = Hidden;}
		|	('$\\') =>
			// Match escapes not in a string like <\n\ufea5>
			'$' (ESC_CHAR[out uc] {builder.Append(uc);} )+ '$'
			{
				Text = builder.ToString();
				$type = LITERAL;
			}
		|	('$!') => COMMENT
			{ $channel = Hidden; }
		|	(	('$if' (' '|'(')) =>
				'$if' (' ')* '(' exp=IF_EXPR ')$'
				{
					Text = "if(" + $exp.text + ")";
					$type = TemplateParser.IF;
				}
				( ('\r')? '\n' )? // ignore any newline right after an IF
			|	('$elseif' (' '|'(')) =>
				'$elseif' (' ')* '(' exp=IF_EXPR ')$'
				{
					Text = "elseif(" + $exp.text + ")";
					$type = TemplateParser.ELSEIF;
				}
				( ('\r')? '\n' )? // ignore any newline right after an IF
			|	('$else$') =>
				'$else$'
				{
					Text = "else";
					$type = (TemplateParser.ELSE);
				}
				( ('\r')? '\n' )? // ignore any newline right after an ELSE
			|	('$endif$') =>
				'$endif$'
				{
					Text = "endif";
					$type = TemplateParser.ENDIF;
				}
				( {startCol==0}? => ('\r')? '\n' )? // ignore after ENDIF if on line by itself

			|	// match $@foo()$ => foo
				// match $@foo$...$@end$ => foo::=...
				('$@') =>
				{
					builder = new System.Text.StringBuilder();
				}
				'$@' ( ch=~('$'|'(') {builder.Append((char)$ch);} )+
				{ t = builder.ToString(); }
				(	'()$'
					{
						$type = TemplateParser.REGION_REF;
					}
				|	'$'
					{
						$type = TemplateParser.REGION_DEF;
						builder.Append("::=");
					}
					( ('\r'|'\n') => ('\r')? '\n' )?
					{atLeft = false;}
					(	{!(UpcomingAtEND(1) || ( input.LA(1) == '\n' && UpcomingAtEND(2) ) || ( input.LA(1) == '\r' && input.LA(2) == '\n' && UpcomingAtEND(3) ))}? =>
						(	('\r'|'\n') => ('\r' {builder.Append('\r');})? '\n' {builder.Append('\n'); atLeft = true;}
						|	ch=. {builder.Append((char)$ch); atLeft = false;}
						)
					)+
					(	('\r'|'\n') => ('\r')? '\n' {atLeft = true;} )?
					(	('$@end$') => '$@end$'
					|	.	{self.Error("missing region "+t+" \$@end$ tag");}
					)
					(	{atLeft}? ('\r')? '\n' )?
				)
				{
					Text = builder.ToString();
				}
			|	'$' EXPR[out subtext] '$'  // (Can't start with '!', which would mean comment)
				{ Text = subtext; }
			)
			{
				//ChunkToken t = new ChunkToken($type, $text, currentIndent);
				//state.token = t;
				state.token = new ChunkToken($type, $text, currentIndent);
			}
		)
	;

LITERAL
@init
{
	int loopStartIndex = $text.Length;
	int col = CharPositionInLine;
	System.Text.StringBuilder builder = new System.Text.StringBuilder();
}
	:	(	'\\'
			(	(EOF) => EOF		{builder.Append("\\");}  // Allow a template that just has a single backslash
			|	(	'$'				{builder.Append("$");}  // allow escaped delimiter
				|	'\\'			{builder.Append("\\");} // always replace \\ with \
				|	ch=~('$'|'\\')	{builder.Append("\\" + (char)$ch);}  // otherwise ignore escape char
				)
			)
		|	ind=INDENT
			{
				loopStartIndex = builder.Length;
				col = CharPositionInLine - $ind.text.Length;

				builder.Append( $ind.text );
				if ( col==0 && input.LA(1)=='$' )
				{
					// store indent in ASTExpr not in a literal
					currentIndent=$ind.text;
					//text.setLength(loopStartIndex); // reset length to wack text
					builder.Length = loopStartIndex; //= $text.Substring( 0, loopStartIndex );
				}
				else
				{
					currentIndent=null;
				}
			}
		|	ch=~('$'|'\r'|'\n'|'\\'|' '|'\t') {builder.Append((char)$ch);}
		)+
		{Text = builder.ToString();}
		{if ($text.Length==0) {$channel = Hidden;}} // pure indent?
	;

fragment
EXPR[out string _text]
@init
{
	_text = string.Empty;
	string subtext = string.Empty;
	System.Text.StringBuilder builder = new System.Text.StringBuilder();
}
	:	(	ESC							{builder.Append($ESC.text);}
		|	st=SUBTEMPLATE				{builder.Append($st.text);}
		|	(	'='						{builder.Append('=');}
			|	'+'						{builder.Append('+');}
			)
			(	TEMPLATE[out subtext]	{builder.Append(subtext);}
			|	st=SUBTEMPLATE			{builder.Append($st.text);}
			|	ch=~('"'|'<'|'{')		{builder.Append((char)$ch);}
			)
		|	ch=~('\\'|'{'|'='|'+'|'$')	{builder.Append((char)$ch);}
		)+
		{$_text = builder.ToString();}
	;

fragment
TEMPLATE[out string _text]
@init
{
	_text = string.Empty;
	System.Text.StringBuilder builder = new System.Text.StringBuilder();
}
	:	'"'
		{builder.Append('"');}
		(	ESC						{builder.Append($ESC.text);}
		|	ch=~('\\'|'"')	{builder.Append((char)$ch);}
		)*
		'"'
		{
			builder.Append('"');
			$_text = builder.ToString();
		}
	|	'<<'
		{
			builder.Append("<<");
		}
	 	(	('\r'|'\n') => ('\r')? '\n'
	 	)? // consume 1st \n
		(	('\r'? '\n>>') => ('\r')? '\n'	// kill last \r\n
		|	ch=. {builder.Append((char)$ch);}
		)*
		'>>'
		{
			builder.Append(">>");
			$_text = builder.ToString();
		}
	;

fragment
IF_EXPR
	:	(	ESC
		|	SUBTEMPLATE
		|	NESTED_PARENS
		|	~('\\'|'{'|'('|')')
		)+
	;

fragment
ESC_CHAR[out char uc]
@init
{
	$uc = '\0';
}
	:	'\\n' {$uc = '\n';}
	|	'\\r' {$uc = '\r';}
	|	'\\t' {$uc = '\t';}
	|	'\\ ' {$uc = ' ';}
	|	'\\u' a=HEX b=HEX c=HEX d=HEX
		{$uc = (char)int.Parse($a.text+$b.text+$c.text+$d.text, System.Globalization.NumberStyles.AllowHexSpecifier);}
	;

fragment
ESC
	:	'\\' . // ('$'|'n'|'t'|'"'|'\''|':'|'{'|'}')
	;

fragment
HEX
	:	'0'..'9'|'A'..'F'|'a'..'f'
	;

fragment
SUBTEMPLATE
	:	'{'
		(	SUBTEMPLATE
		|	ESC
		|	~('{'|'\\'|'}')
		)*
		'}'
	;

fragment
NESTED_PARENS
	:	'('
		(	NESTED_PARENS
		|	ESC
		|	~('('|'\\'|')')
		)+
		')'
	;

fragment
INDENT
	:	(' ' | '\t')+
	;

fragment
COMMENT
@init
{
	int startCol = CharPositionInLine;
}
	:	'$!' .* '!$' ( {startCol==0}? => ('\r')? '\n' )?
	;

fragment
LINE_BREAK
	:	'$\\\\$' INDENT? '\r'? '\n' INDENT?
	;
