| 
									
										
										
										
											2020-06-03 16:24:41 +02:00
										 |  |  | function lexer(str) { | 
					
						
							|  |  |  |     str = str.toLowerCase(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const expressionTokens = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let quotes = false; | 
					
						
							|  |  |  |     let currentWord = ''; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function isOperatorSymbol(chr) { | 
					
						
							|  |  |  |         return ['=', '*', '>', '<', '!'].includes(chr); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function previousOperatorSymbol() { | 
					
						
							|  |  |  |         if (currentWord.length === 0) { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             return isOperatorSymbol(currentWord[currentWord.length - 1]); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function finishWord() { | 
					
						
							|  |  |  |         if (currentWord === '') { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-03 17:11:03 +02:00
										 |  |  |         expressionTokens.push(currentWord); | 
					
						
							| 
									
										
										
										
											2020-06-03 16:24:41 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         currentWord = ''; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (let i = 0; i < str.length; i++) { | 
					
						
							|  |  |  |         const chr = str[i]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (chr === '\\') { | 
					
						
							|  |  |  |             if ((i + 1) < str.length) { | 
					
						
							|  |  |  |                 i++; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 currentWord += str[i]; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else { | 
					
						
							|  |  |  |                 currentWord += chr; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             continue; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (['"', "'", '`'].includes(chr)) { | 
					
						
							|  |  |  |             if (!quotes) { | 
					
						
							| 
									
										
										
										
											2020-06-03 17:11:03 +02:00
										 |  |  |                 if (previousOperatorSymbol()) { | 
					
						
							|  |  |  |                     finishWord(); | 
					
						
							| 
									
										
										
										
											2020-06-03 16:24:41 +02:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2020-06-03 17:11:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 quotes = chr; | 
					
						
							| 
									
										
										
										
											2020-06-03 16:24:41 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |             else if (quotes === chr) { | 
					
						
							|  |  |  |                 quotes = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 finishWord(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else { | 
					
						
							|  |  |  |                 // it's a quote but within other kind of quotes so it's valid as a literal character
 | 
					
						
							|  |  |  |                 currentWord += chr; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             continue; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (!quotes) { | 
					
						
							|  |  |  |             if (currentWord.length === 0 && (chr === '#' || chr === '~')) { | 
					
						
							|  |  |  |                 currentWord = chr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else if (chr === ' ') { | 
					
						
							|  |  |  |                 finishWord(); | 
					
						
							|  |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-06-03 17:11:03 +02:00
										 |  |  |             else if (['(', ')', '.'].includes(chr)) { | 
					
						
							| 
									
										
										
										
											2020-06-03 16:24:41 +02:00
										 |  |  |                 finishWord(); | 
					
						
							|  |  |  |                 currentWord += chr; | 
					
						
							|  |  |  |                 finishWord(); | 
					
						
							|  |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-06-03 17:11:03 +02:00
										 |  |  |             else if (previousOperatorSymbol() !== isOperatorSymbol(chr)) { | 
					
						
							| 
									
										
										
										
											2020-06-03 16:24:41 +02:00
										 |  |  |                 finishWord(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 currentWord += chr; | 
					
						
							|  |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         currentWord += chr; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     finishWord(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-03 17:11:03 +02:00
										 |  |  |     return expressionTokens; | 
					
						
							| 
									
										
										
										
											2020-06-03 16:24:41 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default { | 
					
						
							|  |  |  |     lexer | 
					
						
							|  |  |  | } |