Summary Files Commits Branches @commit @diff @stats
path: root / src / StringConsumer
diff options
context:
space:
mode:
Commitac5b073138ed3cd8073eed8eea053a1f4799fc25 (patch)
Author
 Syndamia <kamen@syndamia.com>
2025-11-01 16:45:32 +0200
Committer
 Syndamia <kamen@syndamia.com>
2025-11-01 16:46:02 +0200
Filesb1a6dc7f14a0944b6a3dadf36ca5e315d832e3a3 /src/StringConsumer
cool-self-hosted-ac5b073138ed3cd8073eed8eea053a1f4799fc25.tar
cool-self-hosted-ac5b073138ed3cd8073eed8eea053a1f4799fc25.tar.gz
cool-self-hosted-ac5b073138ed3cd8073eed8eea053a1f4799fc25.zip
Parentb43c94ca62a747f2f9fb222cdf887c041dd544bb (diff)
feat(StringConsumer): Move most whitespace consumer logic into a more generic class
diff --git a/src/StringConsumer/IStringConsumer.cl b/src/StringConsumer/IStringConsumer.cl
index 20a4f3a..ee5c9fc 100644
--- a/src/StringConsumer/IStringConsumer.cl
+++ b/src/StringConsumer/IStringConsumer.cl
@@ -1,4 +1,8 @@
class IStringConsumer inherits System {
+ init() : IStringConsumer {
+ self
+ };
+
consume(s: String) : Int {
not_implemented_retint("consume")
};
diff --git a/src/StringConsumer/IWordConsumer.cl b/src/StringConsumer/IWordConsumer.cl
new file mode 100644
index 0000000..1146710
--- /dev/null
+++ b/src/StringConsumer/IWordConsumer.cl
@@ -0,0 +1,63 @@
+class WordList inherits System {
+ word: String;
+ next: WordList;
+
+ init(value: String, tail: Object) : WordList {{
+ assert(not (isvoid value), "init", "value argument cannot be void!");
+ assert(isvoid word, "init", "List has already been initialised!");
+
+ word <- value;
+ if isvoid tail then
+ void
+ else
+ case tail of
+ wl: WordList => next <- wl;
+ o: Object => assert(false, "init", "tail argument can be either a ConsumerList or void!");
+ esac
+ fi;
+
+ self;
+ }};
+
+ push(value: String) : WordList {{
+ assert(not (isvoid value), "push", "value argument cannot be void!");
+
+ if isvoid word
+ then
+ word <- value
+ else if isvoid next
+ then
+ next <- (new WordList).init(value, void)
+ else
+ next.push(value)
+ fi
+ fi;
+ self;
+ }};
+
+ any_starts(s: String, start: Int) : Bool {
+ if isvoid word
+ then false
+ else if s.substr(start, word.length()) = word
+ then true
+ else if isvoid next
+ then false
+ else next.any_starts(s, start)
+ fi
+ fi
+ fi
+ };
+};
+
+class IWordConsumer inherits IStringConsumer {
+ matched_words: WordList <- new WordList;
+
+ consume(s: String) : Int {{
+ consumed_length <- 0;
+ while matched_words.any_starts(s, consumed_length)
+ loop
+ consumed_length <- consumed_length + 1
+ pool;
+ consumed_length;
+ }};
+};
diff --git a/src/StringConsumer/StringConsumerEngine.cl b/src/StringConsumer/StringConsumerEngine.cl
index dc7e202..585b76d 100644
--- a/src/StringConsumer/StringConsumerEngine.cl
+++ b/src/StringConsumer/StringConsumerEngine.cl
@@ -1,6 +1,6 @@
class StringConsumerEngine inherits System {
consumers: ConsumerList <- (new ConsumerList)
- .push(new WhitespaceConsumer)
+ .push((new WhitespaceConsumer).init())
;
str: String;
diff --git a/src/StringConsumer/WhitespaceConsumer.cl b/src/StringConsumer/WhitespaceConsumer.cl
index 4c0b8b5..8f20385 100644
--- a/src/StringConsumer/WhitespaceConsumer.cl
+++ b/src/StringConsumer/WhitespaceConsumer.cl
@@ -1,12 +1,10 @@
-class WhitespaceConsumer inherits IStringConsumer {
- consume(s: String) : Int {{
- consumed_length <- 0;
- while or(s.substr(consumed_length, 1) = " ",
- s.substr(consumed_length, 1) = "\t")
- loop
- consumed_length <- consumed_length + 1
- pool;
- consumed_length;
+class WhitespaceConsumer inherits IWordConsumer {
+ init() : IStringConsumer {{
+ matched_words <- matched_words
+ .push(" ")
+ .push("\t")
+ ;
+ self;
}};
token() : IToken {