/* ----------------------------------------------------------------------------- Copyright 2023-2024 Kevin P. Barry Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ----------------------------------------------------------------------------- */ // Author: Kevin P. Barry [ta0kira@gmail.com] $ModuleOnly$ // Tokenizes with context #c and generates values of type #v. @value interface Tokenizer<#c|#v> { tokenize (TextStream, #c) -> (optional #v) } // Supports generic formatting based on label type #l and content type #x. @value interface FormattedToken<|#l, #x> { formatWith<#f> (TokenFormatter<#l, #x, #f>) -> (#f) } // Generates #f values by formatting with label type #l and content type #x. @value interface TokenFormatter<#l, #x|#f|> { formatLeaf (#l label:, #x value:) -> (#f) createSection (#l label:) -> ([Append<#f> & Build<#f>]) } // Input adapter for parsing text input. concrete TextStream { @type new (String) -> (TextStream) @value current () -> (optional Char) @value tokenSize () -> (Int) @value preview () -> (String) @value currentLine () -> (Int) @value currentChar () -> (Int) @value atEnd () -> (Bool) @value forward () -> (TextStream) @value reset () -> (TextStream) @value take () -> (String) } // Tokenizes all content in a TextStream. concrete StreamTokenizer<#c|#v> { @category new<#c, #v> (#c context:, Tokenizer<#c, #v> tokenizer:) -> (StreamTokenizer<#c, #v>) @value tokenizeAll (TextStream, Append<#v>) -> (#self) } // Serializes parsed data without formatting. concrete UnformattedFormatter { refines TokenFormatter @type new () -> (#self) } // Helpers for reasoning about Char values. concrete CharType { @type lower (Char) -> (Bool) @type upper (Char) -> (Bool) @type digit (Char) -> (Bool) @type binDigit (Char) -> (Bool) @type octDigit (Char) -> (Bool) @type hexDigit (Char) -> (Bool) @type alphaNum (Char) -> (Bool) @type whitespace (Char) -> (Bool) @type binChars () -> (SetReader) @type octChars () -> (SetReader) @type decChars () -> (SetReader) @type hexChars () -> (SetReader) @type oneOf (Char, DefaultOrder) -> (Bool) @type escapeBreaks (DefaultOrder) -> (String) }