parse method

Future<Ruleset> parse (
  1. String str
)

Parse an input string into an abstract syntax tree.

str is a string containing 'less' markup

Implementation

Future<Ruleset> parse(String str) {
  Ruleset root;
  var _str = str;

  fileInfo ??= context.currentFileInfo;
  imports ??= ImportManager(context, fileInfo);
  final processOptions = <String, dynamic>{
    'context': context,
    'imports': imports,
    'fileInfo': fileInfo
  };

  if (context.pluginManager != null) {
    context.pluginManager
        .getPreProcessors()
        .forEach((Processor preProcessor) {
      _str = preProcessor.process(_str, processOptions);
    });
  }

  if (globalVars.isNotEmpty || banner.isNotEmpty) {
    preText = '$banner$globalVars'.replaceAll(RegExp(r'\r\n?'), '\n');

    if (!imports.contentsIgnoredChars.containsKey(fileInfo.filename)) {
      imports.contentsIgnoredChars[fileInfo.filename] = 0;
    }
    imports.contentsIgnoredChars[fileInfo.filename] += preText.length;
  }

  // Remove potential UTF Byte Order Mark
  _str = _str.replaceAll('\r\n', '\n').replaceAll(RegExp(r'^\uFEFF'), '');
  _str = '$preText$_str$modifyVars';

  imports.contents[fileInfo.filename] = _str;

  context
    ..imports = imports
    ..input = _str;

  // Start with the primary rule.
  // The whole syntax tree is held under a Ruleset node,
  // with the `root` property set to true, so no `{}` are
  // output. The callback is called when the input is parsed.

  try {
    parsers = Parsers(_str, context);
    root = Ruleset(null, parsers.primary())
      ..root = true
      ..firstRoot = true;
    parsers.isFinished();

    IgnitionVisitor().run(root); // @options directive process

    if (context.processImports) {
      return ImportVisitor(imports)
          .runAsync(root)
          .then((_) => Future<Ruleset>.value(root))
          .catchError((Object e) {
        throw LessExceptionError(
            LessError.transform(e, type: 'Import', context: context));
      });
    }
  } catch (e, s) {
    return Future<Ruleset>.error(LessExceptionError(
        LessError.transform(e, stackTrace: s, context: context)));
  }

  return Future<Ruleset>.value(root);
}