Class: Parser

markus.Parser

Marklang Parser

new markus.Parser (loadType)

Name Type Default Description
loadType string ajax optional

Method of loading markfile

Example
const parser = new markus.Parser();
await parser.parseMarkfile('mark.view');
> [{type="elementNode", element: 'app', props: {...}, presets: [...]}]

Members

loadType string

Method of loading markfile.

Methods

generateTree (presets)Array

Generates AST from presets based on type and depth properties of a preset.

Name Type Description
presets Array.<Preset>

List presets

Returns:
Type Description
Array AST presets
Example
parser.generateTree([
  {type: 'elementNode', depth: 0, element: 'app'},
  {type: 'elementNode', depth: 1, element: 'text'},
  {type: 'propNode', depth: 2, name: 'text', value=''}
  {type: 'valueNode', depth: 3, value='TEXT NODE'}
])

> {type: 'elementNode', depth: 0, element: 'app', presets: [{
    type: 'elementNode', depth: 1, element: 'text', props: {
      text: 'TEXT NODE'
    }}
  }]
}

getAttr (line)Array.<string>

Retrieves an element property from a @prop value and $prop value structure

Name Type Description
line string

line with marklang markup

Returns:
Type Description
Array.<string> attr from markline. [attrType, attrNane, attrValue]
Example
parser.getAttr("@prop on")
> ['propNode', "prop", true]

parser.getAttr("@func(on, 20, text)")
> ['propNode', "attr", [true, 20, 'text']]

getComment (line)string

Getes comment from markline

Name Type Description
line string

line with marklang markup

Returns:
Type Description
string Comment from markline
Example
getComment('elm.tag // some comment')
> // some comment

getDepth (line)number

Finds the depth of the entry element. Calculated by the number of tabs at the beginning of the line.

Name Type Description
line string

line with marklang markup

Returns:
Type Description
number depth
Example
parser.getDepth("\t\t\t\t")
> 4

getElement (line)string

Get element name

Name Type Description
line string

line with marklang markup

Returns:
Type Description
string element name from markline
Example
parser.getElement("sprite.tag#id(prop=data)")
> "sprite"

getId (line)string

Extracts the element id from the #id_name construction

Name Type Description
line string

line with marklang markup

Returns:
Type Description
string element id from markline
Example
parser.getId("el.tag1#cat.tag2.tag3")
> "cat"

getImports (data)Array.<string>

Finds all import requests in the file from the import path construction

Name Type Description
data Array.<string>

lines array with marklang markup

Returns:
Type Description
Array.<string> imports data
Example
parser.getImports([
  'import resources.mark',
  'app(w=1920, h=900)',
  ' import scenes.mark'
].join('\n'));
> ['resources.mark', 'styles.mark']

getInlineAttrs (line)Object

Retrieves all element properties from the structure (prop = data, ...)

Name Type Description
line string

line with marklang markup

Returns:
Type Description
Object element attrs from markline
Example
parser.getInlineAttrs("el(texture=cat.png, font= Bebas Neue, visible = off, some.point.x = .4)")
> {texture: "cat.png", font="Bebas Neue", visible: false, some: {point: {x: .4}}}

getTags (line)Array.<string>

Extracts all element tags from .tag_name construction

Name Type Description
line string

line with marklang markup

Returns:
Type Description
Array.<string> element tags from markline
Example
parser.getTags("el.tag1#id.tag2.tag3")
> ["tag1", "tag2", "tag3"]

getValue (line)string

Extract text value from the | .+ construction

Name Type Description
line string

line with marklang markup

Returns:
Type Description
string element value from markline
Example
parser.getValue("| SOME VALUE ");
> "SOME VALUE "

imports (pathes)Promise

Loaded markfiles from array pathes

Name Type Description
pathes Array.<string>

Patches to markfiles

Returns:
Type Description
Promise
Example
await parser.imports(['./mark.view', './resources.mark']);
> [{name: 'mark.view', path: './mark.view', data: '...'}, {...}]

parseMarkfile (filepath)Promise

Parse markfile to AST presets

Name Type Description
filepath string

file path to markfile

Returns:
Type Description
Promise Return promise with ast presets
Example
await parser.parseMarkfile(['./mark.view');
> [{type="elementNode", element: 'app', props: {...}, presets: [...]}]

parsePreset (line)Preset

Parse marklang string to preset

Name Type Description
line string

String with marklang markup

Returns:
Type Description
Preset
Example
parser.parsePreset('    text.tag#id(obj.visible = yes) | VALUE')
> {
  type: 'elementNode',
  depth: 2
  element: 'text',
  id: 'id',
  tags: ['tag'],
  props: {obj: {visible: true}},
  value: 'VALUE'
}

parser.parsePreset('    | VALUE TEXT')
> {
  depth: 2,
  type: 'valueNode',
  value: 'VALUE TEXT'
}

parser.parsePreset('  @prop .3324')
> {
  depth: 1,
  type: 'propNode',
  typeAttr: 'prop',
  name: 'prop',
  value: 0.3324
}

parser.parsePreset('  @move(10, 30)')
> {
  depth: 1,
  type: 'propNode',
  typeAttr: 'method',
  name: 'move',
  args: [10, 30]
}

parsePresets (lines)Array.<Preset>

Parse marklang lines array to presets. Calls parser.parsePreset (line [i])

Name Type Description
lines Array.<string>

Strings with marklang markup

Returns:
Type Description
Array.<Preset>

parseQuery (query)Object

Parse query selector to object

Name Type Description
query string

Query selector in marklang markup

Returns:
Type Description
Object Query selector in object
Example
parser.parseQuery('element.tag.tag2#id');
> {element: 'element', tags: ['tag', 'tag2'], id: 'id'}

parseValue (value)any

Convert value from props with support js types

Name Type Description
value string
Returns:
Type Description
any Parsed value
Example
parser.parseValue('no'|'off'|'false');
> false
parser.parseValue('yes'|'on'|'true');
> true
parser.parseValue('.5'|'+34'|'-34'|'3.32432');
> Number
parser.parseValue('anystring');
> String

removeComment (line)string

Removes comment from markline

Name Type Description
line string

line with marklang markup

Returns:
Type Description
string markline without comment
Example
removeComment('elm.tag // some comment')
> 'elm.tag'