Index
# Parser Parser uses a tokenizer to cut the text it parses and feeding it to a state like machine to create objects. So you can transform a simple text into a list of objects. > XmlParser parse xml text to create `MMO\core\helpers\XmlNodes` ## Get Started 1. Create a new class extending `\MMO\core\classes\aParser` 2. Initialize the tokenizer with a root domain 3. Feed the text to the tokenizer 4. Process the list of tokens ### Token Token are object containing information about the text it represents. From a token you can get : - its text - its position in the source (absolute, line and column) - the domain in which it has been generated ### Domain A domain describes which token can be generated. It can be composed of other domains and takes care of generating the tokens. There are a list of standard domain already available : - `TokenDomain`: the root domain for any tokenizer. - `SQuoteDomain`: the simple quote domain `'`. - `DQuoteDomain`: the double quote domain `"`. - `BQuoteDomain`: the backtick domain `` ` ``. - `ParenthesisDomain`: the parenthesis domain `()`. - `BracketsDomain`: the bracket domain `[]` - `CurlyBracketsDomain`: the curly bracket domain `{}` - `TagDomain`: the tag domain `<>` - `GroupDomain`: a generic domain composed by a starting string and an ending one. ## The Tokenizer The tokenizer is created with a root domain. This domain defines which token can be generated and which other domains can be encountered. The tokenizer consumes its text and transform it into a list of tokens. At any given time, the current list of token represents what has been tokenized, and the text represents what needs to be tokenized. So in order to get previous text, you need to look at the tokenizer token's text, and to get the next texts, you need to look at the tokenizer current text. The tokenizer look for any starting character that will either open or close a domain or create a new token. - When a domain is opened, it becomes the main domain and only token from this domain can be generated. - When opened, it generated a token from the previous main domain, with the text that led to the opening. - When closed, it generated a token from itself, with the text that led to the closing. The previous main domain becomes the main domain. Domain are stacked during the tokenizing. Only the root domain cannot be unstacked. ## Example ```php $tokenizer = new Tokenizer(new TokenDomain([new DQuoteDomain(), new SQuoteDomain()])); $tokenizer->setText('"text"'); $tokens = iterator_to_array($tokenizer, false);//=> [", text, "] ``` ## References ### See also ## Tags <#parser>, <#tokenizer>