Example #1
0
		///////////////////////////////////////////////////////////////////////////

		private void ParseMacro( int depth, IInput input, StringBuilder sb )
		{
			// ******
			char quoteStartChar = gc.SeqOpenQuote.FirstChar;

			// ******
			while( true ) {
				//
				// first quote check
				//
				if( quoteStartChar == input.Peek() && gc.SeqOpenQuote.Starts(input) ) {
					//
					// GetQuotedText() strips the outer quotes but perserves inner quotes; but
					// first we need to eat the open quote
					//
					gc.SeqOpenQuote.Skip( input );
					sb.Append( gc.GetQuotedText(input, true) );
				}

				// ******				
				char ch = input.Next();

				if( SC.NO_CHAR == ch ) {
					ThreadContext.MacroError( "defmacro/pushmacro: end of input before (#endmacro) found" );
				}
				
				// ******				
				//
				// (#defmacro ...
				// (#pushmacro ...
				//
				if( SC.OPEN_PAREN == ch && input.StartsWith(BLOCK_FOREACH_START) ) {
					//
					// get recursive
					//
					input.Skip( BLOCK_FOREACH_START.Length );

					if( ! char.IsWhiteSpace(input.Peek()) ) {
						ThreadContext.MacroError( "expected white space following \"(#defmacro\"" );
					}
					
					sb.Append( BLOCK_FOREACH_INJECT );
					ParseMacro( 1 + depth, input, sb );
					sb.Append( BLOCK_ENDFOREACH_INJECT );
					continue;
				}

				else if( SC.OPEN_PAREN == ch && input.StartsWith(BLOCK_FOREACH_END) ) {
					input.Skip( BLOCK_FOREACH_END.Length );
					return;
				}

				else {
					sb.Append( ch );
				}
			}
		}
Example #2
0
File: Block.cs Project: jmclain/Nmp
		private void ParseMacro( int depth, IInput input, StringBuilder sb )
		{
			// ******
			char quoteStartChar = gc.SeqOpenQuote.FirstChar;

			// ******
			while( true ) {
				//
				// first quote check
				//
				if( quoteStartChar == input.Peek() && gc.SeqOpenQuote.Starts(input) ) {
					//
					// GetQuotedText() strips the outer quotes but perserves inner quotes; but
					// first we need to eat the open quote
					//
					gc.SeqOpenQuote.Skip( input );
					sb.Append( gc.GetQuotedText(input, true) );
				}

				// ******				
				char ch = input.Next();

				// ******				
				if( SC.NO_CHAR == ch ) {
					ThreadContext.MacroError( "block error: end of input before (#endblock) found" );
				}
				
				// ******				
				//
				// (#block ...
				//
				if( SC.OPEN_PAREN == ch && input.StartsWith(BLOCK_START) ) {
					//
					// get recursive
					//
					input.Skip( BLOCK_START.Length );

					// ******
					sb.Append( BLOCK_INJECT );
					ParseMacro( 1 + depth, input, sb );
					sb.Append( BLOCKEND_INJECT );
					continue;
				}

				else if( SC.OPEN_PAREN == ch && input.StartsWith(BLOCK_END) ) {
					input.Skip( BLOCK_END.Length );
					return;
				}

				else {
					sb.Append( ch );
				}
			}
		}
Example #3
0
		///////////////////////////////////////////////////////////////////////////

		private void ParseMacro( IInput input, StringBuilder sb )
		{
			// ******
			char quoteStartChar = gc.SeqOpenQuote.FirstChar;

			// ******
			while( true ) {
				//
				// first quote check - allows (#endarray) to be embedded in the text
				//
				if( quoteStartChar == input.Peek() && gc.SeqOpenQuote.Starts(input) ) {
					//
					// GetQuotedText() strips the outer quotes but perserves inner quotes; but
					// first we need to eat the open quote
					//
					gc.SeqOpenQuote.Skip( input );
					sb.Append( gc.GetQuotedText(input, true) );
				}

				// ******				
				char ch = input.Next();

				if( SC.NO_CHAR == ch ) {
					ThreadContext.MacroError( "deflist: end of input before (#endlist) found" );
				}
				
				if( SC.OPEN_PAREN == ch && input.StartsWith(ENDLIST) ) {
					input.Skip( ENDLIST.Length );
					return;
				}

				else {
					sb.Append( ch );
				}
			}
		}
Example #4
0
File: ETB.cs Project: jmclain/Nmp
		/////////////////////////////////////////////////////////////////////////////

		public MacroExpression ParseExpression( IInput input )
		{
			// ******
			var exList = new MacroExpression( new NmpStringList() );

			// ******
			//
			// first token is the name of the macro
			//
			string	token = rootName;
			bool		haveFreshToken = true;

			// ******
			while( true ) {
				bool isRootNode = 0 == exList.Count;
				bool allDone = false;

				char ch = input.Peek();

				if( haveFreshToken ) {
					allDone = HandleFreshToken( isRootNode, token, char.IsWhiteSpace(ch), exList, input );
					haveFreshToken = false;
				}
				
				if( SC.DOT == ch ) {
					//
					// '.'
					//
					input.Skip( 1 );
					token = GetToken( input );
				
					if( string.IsNullOrEmpty(token) ) {
						//
						// does not return
						//
						UnexpectedCharacter( input.Peek(), "expecting identifier (member name)" );
					}
				
					// ******
					haveFreshToken = true;
				}

// jpm 11 April 13; change back to NOT check for (# because its more correct that what
// we ended up with - that is, without the check its possible for the user to create a
// macro (across multiple lines using '-' at end of line and comments ;;) where we
// get "something()(#defmacro..)" glued together as the result of a macro atempting to
// call a method - not what was intended
//
				//else if( SC.OPEN_PAREN == ch && SC.HASH != input.Peek(1) ) {
				else if( SC.OPEN_PAREN == ch ) {
					//
					// '(' NOT "(#"
					//
					input.Skip( 1 );
					exList.Add( MethodCall(isRootNode, token, haveFreshToken, input) );
					haveFreshToken = false;
				}

				else if( SC.OPEN_BRACKET == ch ) {
					//
					// '['
					//
					input.Skip( 1 );
					exList.Add( IndexMember(isRootNode, token, haveFreshToken, input) );
					haveFreshToken = false;                                               
				}

				else if( SC.ATCHAR == ch && SC.OPEN_BRACKET == input.Peek(1) ) {
					input.Skip( 2 );
					ParseMacroOptions( input, exList.MacroInstructions );
				}
				else {
					//
					// end of expression
					//

					/*	

						should put whatever we do here just before the end of the
						method - get it out of here for cleanthlyness sake


						if is altInvocationExpression and we did not detect it as a method in HandleFreshToken()
							
							we've hit ')' or some char we don't know what to do with

						we could gather up "... )" and add to MacroOptions

							or error

							or just eat it
					*/

					//if( isAltInvocationExpression && ! allDone ) {
					//	//NmpStringList strArgs = argScanner( input, RecognizedCharType.CloseParenChar );
					//	NmpStringList strArgs = scanner.ArgScanner( input, RecognizedCharType.CloseParenChar );
					//}

					if( isAltInvocationExpression ) {
						if( ! allDone ) {
							NmpStringList strArgs = scanner.ArgScanner( input, RecognizedCharType.CloseParenChar );
						}

						if( SC.ATCHAR == input.Peek() && SC.OPEN_BRACKET == input.Peek(1) ) {
							input.Skip( 2 );
							ParseMacroOptions( input, exList.MacroInstructions );
						}

					}

					break;
				}

				// ******
				//
				// since there is no member/token name for the result of an operation
				// we need to create one in case of cascading operations
				//
				if( !haveFreshToken ) {
					token = "result of " + token;
				}
			}

			// ******
			//return 1 == exList.Count ? exList[0] : exList;
			return exList;
		}
Example #5
0
		/////////////////////////////////////////////////////////////////////////////

		public override string ParseBlock( Expression exp, IInput input )
		{
			// ******
			//
			// remove any newline following the closing paren (#macro ...)
			//
			if( SC.NEWLINE == input.Peek() ) {
				input.Skip( 1 );
			}

			// ******
			StringBuilder macroText = new StringBuilder();
			ParseMacro( input, macroText );

			// ******
			//
			// remove any newline following the closing paren for the (#endmacro)
			//
			if( SC.NEWLINE == input.Peek() ) {
				input.Skip( 1 );
			}

			// ******
			return macroText.ToString();
		}
Example #6
0
		///////////////////////////////////////////////////////////////////////////

		private void ParseMacro( int depth, IInput input, StringBuilder sb )
		{
			// ******
			char quoteStartChar = gc.SeqOpenQuote.FirstChar;

			// ******
			while( true ) {
				//
				// first quote check
				//
				if( quoteStartChar == input.Peek() && gc.SeqOpenQuote.Starts(input) ) {
					//
					// GetQuotedText() strips the outer quotes but perserves inner quotes; but
					// first we need to eat the open quote
					//
					gc.SeqOpenQuote.Skip( input );
					sb.Append( gc.GetQuotedText(input, true) );
				}

				// ******				
				char ch = input.Next();

				if( SC.NO_CHAR == ch ) {
					ThreadContext.MacroError( "defmacro/pushmacro: end of input before (#endmacro) found" );
				}
				
				// ******				
				//
				// (#defmacro ...
				// (#pushmacro ...
				//
				if( SC.OPEN_PAREN == ch && input.StartsWith(DEFMACRO_START) ) {
					//
					// get recursive
					//
					input.Skip( DEFMACRO_START.Length );

					if( ! char.IsWhiteSpace(input.Peek()) ) {
						ThreadContext.MacroError( "expected white space following \"(#defmacro\"" );
					}
					
					sb.Append( DEFMACRO_INJECT );
					ParseMacro( 1 + depth, input, sb );
					sb.Append( ENDMACRO_INJECT );
					continue;
				}

		//
		// easier / shorter way to do this - duping code from above
		//
				else if( SC.OPEN_PAREN == ch && input.StartsWith(PUSHMACRO_START) ) {
					//
					// get recursive
					//
					input.Skip( PUSHMACRO_START.Length );

					if( ! char.IsWhiteSpace(input.Peek()) ) {
						ThreadContext.MacroError( "expected white space following \"(#defmacro\"" );
					}
					
					sb.Append( PUSHMACRO_INJECT );
					ParseMacro( 1 + depth, input, sb );
					sb.Append( ENDMACRO_INJECT );
					continue;
				}

					//case SC.EMBED_BEGIN_INBLOCK_EXPAND_CHAR:
					//	if( 0 == depth ) {
					//		//
					//		// only for the outermost macro code
					//		//
					//		sb.Append( HandleEmbedMacroBlock(input) );
					//		continue;
					//	}
					//	break;

				else if( SC.OPEN_PAREN == ch && input.StartsWith(ENDMACRO) ) {
					input.Skip( ENDMACRO.Length );
					return;
				}

				else {
					sb.Append( ch );
				}
			}
		}
Example #7
0
File: If.cs Project: jmclain/Nmp
		// (#if bool_expression )
		//
		// ... (#elseif ...) / (#else)
		//
		// (#endif)

//		///////////////////////////////////////////////////////////////////////////
//
//		
//string	IfElseEndifPatterns =
//
//@"(?x)\A
//(
//\(\#if[ \t]|
//\(\#elseif[ \t]|
//\(\#else\)|
//\(\#endif\)|
//
//)
//";
//
		
		///////////////////////////////////////////////////////////////////////////

		private StringBuilder IfProcessor( IInput input, bool processing, int depth )
		{
		StringBuilder	result = new StringBuilder();
		bool	inElse = false;
		bool done = false;
		
		/*
		
			processing says whether we are saving text or not
			
				text is only saved if processing is true
			
			depth is how may if statements deep we are into the process
			
				depth zero is the first level after examining the truth of the outermost if, at this
				level processing can be toggled by the else statement and NOT have an else token reinjected
				into the code
				
				at levels greather than zero if/else/endif tokens are reinjected into the code to be
				processed after the text has been pushed back onto the input and is scanned again
		
		
		*/

			// ******
			char quoteStartChar = gc.SeqOpenQuote.FirstChar;
			
			// ******
			while( true ) {
				//
				// first quote check
				//
				if( quoteStartChar == input.Peek() && gc.SeqOpenQuote.Starts(input) ) {
					//
					// GetQuotedText() strips the outer quotes but perserves inner quotes; but
					// first we need to eat the open quote
					//
					gc.SeqOpenQuote.Skip( input );
					string text = gc.GetQuotedText( input, true );

					if( processing ) {
						result.Append( text );
					}
				}

				// ******				
				char ch = input.Next();

				// ******				
				if( SC.NO_CHAR == ch ) {
					ThreadContext.MacroError( "IfHandler: end of input before (#endif) found" );
					return result;
				}
				
				// ******				
				//
				// (#if ...
				//
				if( SC.OPEN_PAREN == ch && input.StartsWith(IF_START) ) {
					//
					// get recursive
					//
					input.Skip( IF_START.Length );

					if( ! char.IsWhiteSpace(input.Peek()) ) {
						ThreadContext.MacroError( "expected white space following \"(#if\"" );
					}
					
					if( processing ) {
						result.Append( IF_INJECT );
					}

					result.Append( IfProcessor(input, processing, 1 + depth) );
					continue;
				}
				
				//
				// (#elseif ...
				//
				else if( SC.OPEN_PAREN == ch && input.StartsWith(ELSEIF_START) ) {
					int argsStart = input.Index;
					input.Skip( ELSEIF_START.Length );
					
					if( ! char.IsWhiteSpace(input.Peek()) ) {
						ThreadContext.MacroError( "expected white space following \"(#elseif\"" );
					}

					if( inElse ) {
						ThreadContext.MacroError( "out of place (#elseif) in (#if ...) statement" );
					}
				
					// ******
					//
					// we're responsible for gather the arguments to `elseif'
					//
					NmpStringList strList = mp.Get<IScanner>().ArgScanner( input, RecognizedCharType.CloseParenChar );
					int argsEnd = input.Index - 1;
				
					if( SC.NEWLINE == input.Peek() ) {
						input.Skip( 1 );
					}
				
					// ******
					if( processing ) {
						if( depth > DEPTH_ZERO ) {
							//
							// reinject elseif token and text: "[token] expression )"
							//
							string text = input.GetText(argsStart, argsEnd);
							result.AppendFormat( "({0}", text );
						}
						else {
							//
							// else DEPTH_ZERO, need to stop processing
							//
							processing = false;
							done = true;
						}
					}
					else if( DEPTH_ZERO == depth && ! done ) {
						////
						//// since we're plowing through the text ourselves we need to 
						//// call out to have the `elseif' arguments evaluated
						////
						//var ah = new ArgumentHandler( mp, "if processor" );
						//ah.Initialize( new Type [] { typeof(bool) }, strList );
						//processing = (bool) ah[ 0 ];
						// 
						// already evaluated when ArgScanner was run
						// 
						processing = 0 == strList.Count ? false : Helpers.IsMacroTrue( strList[0] );
					}
				
					continue;
				}
				
				//
				// (#else)
				//
				else if( SC.OPEN_PAREN == ch && input.StartsWith(ELSE) ) {
					input.Skip( ELSE.Length );

					if( inElse ) {
						ThreadContext.MacroError( "out of place (#else) in (#if ..) statement" );
					}
					
					if( SC.NEWLINE == input.Peek() ) {
						input.Skip( 1 );
					}

					// ******
					inElse = true;
					if( processing && depth > DEPTH_ZERO ) {
						//
						// reinject endif token
						//
						result.Append( ELSE_INJECT );
					}

					// ******
					//
					// toggle what we're doing
					//
					if( ! done && DEPTH_ZERO == depth ) {
						processing = ! processing;
					}
					continue;
				}
			
				//
				// (#endif)
				//
				else if( SC.OPEN_PAREN == ch && input.StartsWith(ENDIF) ) {
					//
					// in theory all done
					//
					input.Skip( ENDIF.Length );

					if( SC.NEWLINE == input.Peek() ) {
						input.Skip( 1 );
					}

					if( processing && depth > DEPTH_ZERO ) {
						//
						// reinject endif token
						//
						result.Append( ENDIF_INJECT );
					}
					
					// ******
					break;
				}
				
				// ******
				if( processing ) {
					result.Append( ch );
				}
			}
			
			// ******
			return result;
		}
Example #8
0
File: If.cs Project: jmclain/Nmp
		/////////////////////////////////////////////////////////////////////////////

		public override string ParseBlock( Expression exp, IInput input )
		{
			// ******
			var args = GetMacroArgsAsTuples( exp ) as NmpTuple<bool>;
			bool initialIfIsTrue = args.Item1;

			//  ******
			string	result = string.Empty;
			StringBuilder text = new StringBuilder();
			bool	processing = initialIfIsTrue;
		
			// ******
			if( SC.NEWLINE == input.Peek() ) {
				input.Skip( 1 );
			}

			// ******
			return IfProcessor( input, processing, DEPTH_ZERO ).ToString();
		}
Example #9
0
		/////////////////////////////////////////////////////////////////////////////

		public virtual RecognizedCharType Next( IInput input, out TokenMap tm )
		{
/*

	powershell support for a leaing $ presumes that Next() is only called by
	NmpScanner and that IsMacroIdentifierStartChar() and IsValidMacroIdentifier()
	are never called to validate a powershell name - because they will not be validated

	THIS IS PROBABLY INVALID

*/


			// ******
			RecognizedCharType rct = TestCharType( input );
			
			//
			// must tread tokens that start with '$' special - should do this check for
			// alt token starts as well but since we're going to disallow alt token starts
			// for macros as we rebuild this thing we'll ignore that
			//
			if(	RecognizedCharType.TokenStartChar == rct
					&& (SC.DOLLAR == input.Peek() || SC.HASH == input.Peek()) 
					&& ! IsValidInnerTokenChar(input.Peek(1)) ) {
				//
				// a token that starts with a $ can ONLY be followed by a '.' or a valid token
				//
				if( SC.DOT != input.Peek(1) ) {
					tm = null;
					return RecognizedCharType.JustAChar;
				}
			}

			// ******
			if( RecognizedCharType.TokenStartChar == rct || RecognizedCharType.AltTokenStartChar == rct ) {
				tm = new TokenMap();

				// ******
				if( RecognizedCharType.AltTokenStartChar == rct ) {
					//
					// skip "(#" to find token
					//
					tm.Token = PeekToken( input, altTokenStart.CountChars );
					tm.MatchLength = altTokenStart.CountChars + tm.Token.Length;
					tm.IsAltTokenFormat = true;
				}
				else {
					tm.Token = PeekToken( input, 0 );
					tm.MatchLength = tm.Token.Length;
					tm.IsAltTokenFormat = false;
				}

				// ******
				return RecognizedCharType.TokenStart;
			}

			// ******
			tm = null;
			return rct;
		}
Example #10
0
		/////////////////////////////////////////////////////////////////////////////

		private RecognizedCharType TestCharType( IInput input )
		{
			// ******
			char ch = input.Peek();
			
			// ******
			if( input.RemainderCount > 2 ) {
				if( altTokenStart.FirstChar == ch && altTokenStart.Matches(input, 0) ) {
					
					char char3 = input.Peek( altTokenStart.CountChars );
					if( IsStartChar(char3) ) {
						return RecognizedCharType.AltTokenStartChar;
					}
				}
			}

			// ******
			return NonAltTokenTestCharType( ch );
		}
Example #11
0
		///////////////////////////////////////////////////////////////////////////////
		//
		//private string PeekPwrShellToken( IInput input, int startIndex, char startChar )
		//{
		//	// ******
		//	StringBuilder sb = new StringBuilder();
		//	sb.Append( startChar );
		//
		//	// ******
		//	//
		//	// skip '$'
		//	//
		//	++startIndex;
		//
		//	for( int i = startIndex; ; i++ ) {
		//		char ch = input.Peek( i );
		//
		//		if( char.IsLetterOrDigit(ch) || '_' == ch ) {
		//			sb.Append( ch );
		//		}
		//		else {
		//			break;
		//		}
		//	}
		//
		//	// ******
		//	return sb.ToString();
		//}
		//
		
		/////////////////////////////////////////////////////////////////////////////
		
		private string PeekToken( IInput input, int startIndex )
		{
			// ******
			char ch = input.Peek( startIndex );

			//
			// if we're allowing $ as a start char then we can't differentiate
			// for powershell - user will just have to use legal powershell names
			// - perhaps can show some error in macro lookup
			//
			////
			//// powershell names are different
			////
			//if( IsPowershellStartChar(ch) ) {
			//	return PeekPwrShellToken( input, startIndex, ch );
			//}

			// ******
			StringBuilder sb = new StringBuilder();
		
			// ******
			//for( int i = startIndex; ; i++ ) {
			//	ch = input.Peek( i );
			//
			//	//
			//	// first char has already been validated so we're just checking for
			//	// any character that is valid within a macro name since that includes
			//	// all characters that can start a macro name
			//	//
			//	if( char.IsLetterOrDigit(ch) || '_' == ch || SC.HASH == ch ) {
			//		sb.Append( ch );
			//	}
			//	else if( PUNCT_TERM_CHAR == ch ) {
			//		//
			//		// last char can be '&' - added so we
			//		// can do things like this (##& and #macro& followed
			//		// immedatly by text
			//		//
			//		sb.Append( ch );
			//		break;
			//	}
			//	else {
			//		break;
			//	}
			//}
		
			//
			// first char already validated
			//
			sb.Append( input.Peek(startIndex) );

			for( int i = 1 + startIndex; ; i++ ) {
				ch = input.Peek( i );
		
				//
				// check for valid char OTHER than the start char
				//
				if( char.IsLetterOrDigit(ch) || '_' == ch || SC.HASH == ch ) {
					sb.Append( ch );
				}
				else if( PUNCT_TERM_CHAR == ch ) {
					//
					// last char can be '&' - added so we
					// can do things like this (##& and #macro& followed
					// immedatly by text
					//
					sb.Append( ch );
					break;
				}
				else {
					break;
				}
			}
		
			// ******
			return sb.ToString();
		}