Skip to content

Instantly share code, notes, and snippets.

@stefc
Created August 11, 2017 06:57
Show Gist options
  • Save stefc/8c58a74d8565ee8f9a43136833ed4dde to your computer and use it in GitHub Desktop.
Save stefc/8c58a74d8565ee8f9a43136833ed4dde to your computer and use it in GitHub Desktop.
DataVaultTokenizer
using System.Collections.Generic;
using Superpower.Model;
using Superpower.Parsers;
using Superpower;
using System;
namespace datavault.dsl {
class DataVaultTokenizer : Tokenizer<DataVaultToken> {
protected override IEnumerable<Result<DataVaultToken>> Tokenize(TextSpan span)
{
var next = SkipWhiteSpacesAndComments(span);
if (!next.HasValue)
yield break;
do
{
// vaild tokens ....
next = SkipWhiteSpacesAndComments(next.Location);
} while (next.HasValue);
}
private Result<char> SkipWhiteSpacesAndComments(TextSpan span)
{
var next = SkipWhiteSpace(span);
next = SkipComments(next);
return SkipWhiteSpace(next.Location);
}
private Result<char> SkipComments(Result<char> result)
{
var next = SkipSingleLineComment(result);
next = SkipWhiteSpace(next.Location);
return SkipMultiLineComment(next);
}
private Result<char> SkipSingleLineComment(Result<char> result)
{
if (result.HasValue && result.Value == '/') {
var next = result.Remainder.ConsumeChar();
if (next.HasValue && next.Value == '/') {
while(next.HasValue && (next.Value != '\n'))
next = next.Remainder.ConsumeChar();
return next;
}
}
return result;
}
private Result<char> SkipMultiLineComment(Result<char> result)
{
if (result.HasValue && result.Value == '/') {
var next = result.Remainder.ConsumeChar();
if (next.HasValue && next.Value == '*') {
bool foundAsterix = false;
while(next.HasValue && !(foundAsterix && next.Value == '/'))
{
foundAsterix = next.Value == '*';
next = next.Remainder.ConsumeChar();
}
if (next.HasValue)
next = next.Remainder.ConsumeChar();
return next;
}
}
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment