UNPKG

pbxproj

Version:

parse & stringify xcode pbxproj format

173 lines (136 loc) 3.51 kB
'use strict'; /* * Module dependencies */ var _ = require('lodash'); var util = require('util'); var CommentStage = require('./comment'); var ParserStage = require('../stage'); /* * Stage for objects type : * * { * foo = bar; * }; */ var ObjectStage = module.exports = function () { ParserStage.apply(this, arguments); _.bindAll(this); this.firstRun = true; }; util.inherits(ObjectStage, ParserStage); /* * Override parsing run cycle */ ObjectStage.prototype.run = function () { var commentStage; if (this.firstRun) this.expect(/\{/, this.open); if (CommentStage.isComment(this.data, this.cursor)) { commentStage = new CommentStage(null, this.data, this.cursor, this.line); commentStage.run(); this.cursor = commentStage.cursor; } this.firstRun = false; ParserStage.prototype.run.apply(this, arguments); }; /* * Event callback : open object */ ObjectStage.prototype.open = function () { this.expect(/[a-zA-Z]{1}/, this.openTag); this.expect(/\}/, this.beginClose); this.run(); }; /* * Event callback : open array when '(' is found */ ObjectStage.prototype.openArray = function () { var arrayCtx = []; var ArrayStage = require('./array'); var arrayStage = new ArrayStage(arrayCtx, this.data, this.cursor - 1, this.line); arrayStage.run(); this.cursor = arrayStage.cursor; this.ctx[this.tag] = arrayCtx; this.expect(/[a-zA-Z]{1}/, this.openTag); this.expect(/\}/, this.beginClose); this.run(); }; /* * Event callback : open new object when '{' is found */ ObjectStage.prototype.openNewObject = function () { var objCtx = {}; var objStage = new ObjectStage(objCtx, this.data, this.cursor - 1, this.line); objStage.run(); this.cursor = objStage.cursor; this.ctx[this.tag] = objCtx; this.expect(/[a-zA-Z]{1}/, this.openTag); this.expect(/\}/, this.beginClose); this.run(); }; /* * Event callback : begin close with '}' */ ObjectStage.prototype.beginClose = function () { this.expect(/\;/, this.endClose); this.run(); }; /* * Event callback : end close with ';' */ ObjectStage.prototype.endClose = function () { this.back(); }; /* * Event callback : begin to parse new tag */ ObjectStage.prototype.openTag = function (car) { this.tag = car; this.expect(/[a-zA-Z0-9]{1}/, this.feedTag); this.expect(/=/, this.closeTag); this.run(); }; /* * Event callback : feed current tag name */ ObjectStage.prototype.feedTag = function (car) { this.tag += car; this.expect(/[a-zA-Z0-9]{1}/, this.feedTag); this.expect(/=/, this.closeTag); this.run(); }; /* * Event callback : end tag feeding, wait for value */ ObjectStage.prototype.closeTag = function (car) { this.expect(/\(/, this.openArray); this.expect(/\{/, this.openNewObject); this.expect(/[^\;\s]{1}/, this.openValue); this.run(); }; /* * Event callback : starts to read tag value */ ObjectStage.prototype.openValue = function (car) { this.value = car; this.expect(/[^\;\s]{1}/, this.feedValue); this.run(); }; /* * Event callback : feed current tag value */ ObjectStage.prototype.feedValue = function (car) { this.value += car; this.expect(/[^\;\s]{1}/, this.feedValue); this.expect(/\;/, this.closeValue); this.run(); }; /* * Event callback : end feeding current tag value, wait end or new tag */ ObjectStage.prototype.closeValue = function (car) { this.ctx[this.tag] = this.value; this.expect(/[a-zA-Z]{1}/, this.openTag); this.expect(/\}/, this.beginClose); this.run(); };