~indicator-multiload/indicator-multiload/trunk

« back to all changes in this revision

Viewing changes to src/expressionparser.vala

  • Committer: Michael Hofmann
  • Date: 2011-09-12 07:37:39 UTC
  • Revision ID: mh21@piware.de-20110912073739-ktk2x0t2243o5a2m
Automatic widest strings for indicator text.

Show diffs side-by-side

added added

removed removed

Lines of Context:
177
177
 
178
178
    private uint index;
179
179
    private string[] tokens;
 
180
    bool widest;
180
181
 
181
182
    public ExpressionEvaluator(Providers providers) {
182
183
        this.providers = providers;
325
326
        case 1:
326
327
            bool found = false;
327
328
            try {
328
 
                var result = this.providers.call(token, this.params(), out found);
 
329
                var result = this.providers.call(token, this.params(),
 
330
                        this.widest, out found);
329
331
                if (!found)
330
332
                    throw error(nameindex, "unknown function");
331
333
                return (sign == -1 ? "-" : "") + result;
362
364
        return string.joinv("", result);
363
365
    }
364
366
 
365
 
    public string evaluate(string[] tokens) {
 
367
    public string evaluate(string[] tokens, bool widest) {
366
368
        this.index = 0;
367
369
        this.tokens = tokens;
 
370
        this.widest = widest;
368
371
        try {
369
372
            return text();
370
373
        } catch (Error e) {
395
398
        return new ExpressionTokenizer().tokenize(expression);
396
399
    }
397
400
 
398
 
    public string evaluate(string[] tokens) {
399
 
        return new ExpressionEvaluator(this.providers).evaluate(tokens);
 
401
    public string evaluate(string[] tokens, out string widest) {
 
402
        var evaluator = new ExpressionEvaluator(this.providers);
 
403
        // TODO this should only be done when we need the widest string
 
404
        widest = evaluator.evaluate(tokens, true);
 
405
        return evaluator.evaluate(tokens, false);
400
406
    }
401
407
 
402
 
    public string parse(string expression) {
403
 
        return evaluate(tokenize(expression));
 
408
    public string parse(string expression, out string widest) {
 
409
        return evaluate(tokenize(expression), out widest);
404
410
    }
405
411
}
406
412