Skip to content

Commit 1e54182

Browse files
author
Kapil Borle
committed
Add method to get tokens corresponding to an ast
1 parent 76635b9 commit 1e54182

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

Engine/TokenOperations.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,31 @@ var tokenElse
209209
}
210210
}
211211

212+
public static IEnumerable<Token> GetTokens(Ast outerAst, Ast innerAst, Token[] outerTokens)
213+
{
214+
ThrowIfNull(outerAst, nameof(outerAst));
215+
ThrowIfNull(innerAst, nameof(innerAst));
216+
ThrowIfNull(outerTokens, nameof(outerTokens));
217+
218+
// check if inner ast belongs in outerAst
219+
var foundAst = outerAst.Find(x => x.Equals(innerAst), true);
220+
if (foundAst == null)
221+
{
222+
// todo localize
223+
throw new ArgumentException(String.Format("innerAst cannot be found within outerAst"));
224+
}
225+
226+
var tokenOps = new TokenOperations(outerTokens, outerAst);
227+
return tokenOps.GetTokens(innerAst);
228+
}
229+
230+
private static void ThrowIfNull<T>(T param, string paramName)
231+
{
232+
if (param == null)
233+
{
234+
throw new ArgumentNullException(paramName);
235+
}
236+
}
212237
private IEnumerable<Token> GetTokens(Ast ast)
213238
{
214239
int k = 0;

0 commit comments

Comments
 (0)