Skip to content

Commit

Permalink
Internal: Formula parsing marches through the input string
Browse files Browse the repository at this point in the history
This is better and more sensible than the previous process-suffixes
plan.
  • Loading branch information
kohler committed Jan 14, 2025
1 parent a768c9a commit 1c9c571
Show file tree
Hide file tree
Showing 8 changed files with 412 additions and 365 deletions.
684 changes: 367 additions & 317 deletions src/formula.php

Large diffs are not rendered by default.

29 changes: 14 additions & 15 deletions src/formulas/f_author.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
// formulas/f_author.php -- HotCRP helper class for formula expressions
// Copyright (c) 2009-2024 Eddie Kohler; see LICENSE.
// Copyright (c) 2009-2025 Eddie Kohler; see LICENSE.

class Author_Fexpr extends Fexpr {
private $matchtype;
Expand All @@ -19,23 +19,22 @@ function __construct(FormulaCall $ff, Formula $formula) {
$this->matchidx = count(self::$matchers) - 1;
}
}
static function parse_modifier(FormulaCall $ff, $arg, $rest, Formula $formula) {
if ($ff->modifier === null && !str_starts_with($arg, ".")) {
if (str_starts_with($arg, ":")) {
$arg = substr($arg, 1);
}
$csm = new ContactSearch(ContactSearch::F_TAG, $arg, $formula->user);
if (!$csm->has_error()) {
$ff->modifier = $csm->user_ids();
} else if (!str_starts_with($arg, "#")) {
$ff->modifier = Text::star_text_pregexes($arg);
} else {
return false;
}
return true;
static function parse_modifier(FormulaCall $ff, $arg, Formula $formula) {
if ($ff->modifier !== null || str_starts_with($arg, ".")) {
return false;
}
if (str_starts_with($arg, ":")) {
$arg = substr($arg, 1);
}
$csm = new ContactSearch(ContactSearch::F_TAG, $arg, $formula->user);
if (!$csm->has_error()) {
$ff->modifier = $csm->user_ids();
} else if (!str_starts_with($arg, "#")) {
$ff->modifier = Text::star_text_pregexes($arg);
} else {
return false;
}
return true;
}
function viewable_by(Contact $user) {
return $user->can_view_some_authors();
Expand Down
23 changes: 12 additions & 11 deletions src/formulas/f_pref.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
// formulas/f_pref.php -- HotCRP helper class for formula expressions
// Copyright (c) 2009-2022 Eddie Kohler; see LICENSE.
// Copyright (c) 2009-2025 Eddie Kohler; see LICENSE.

class Pref_Fexpr extends Fexpr {
private $is_expertise;
Expand All @@ -14,16 +14,17 @@ function __construct($ff) {
$this->cids = $ff->modifier;
}
}
static function parse_modifier(FormulaCall $ff, $arg, $rest, Formula $formula) {
if ($ff->modifier === false && !str_starts_with($arg, ".")) {
if (str_starts_with($arg, ":")) {
$arg = substr($arg, 1);
}
$csm = ContactSearch::make_pc($arg, $formula->user);
if (!$csm->has_error()) {
$ff->modifier = $csm->user_ids();
return true;
}
static function parse_modifier(FormulaCall $ff, $arg, Formula $formula) {
if ($ff->modifier !== false || str_starts_with($arg, ".")) {
return false;
}
if (str_starts_with($arg, ":")) {
$arg = substr($arg, 1);
}
$csm = ContactSearch::make_pc($arg, $formula->user);
if (!$csm->has_error()) {
$ff->modifier = $csm->user_ids();
return true;
}
return false;
}
Expand Down
5 changes: 2 additions & 3 deletions src/formulas/f_tag.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
// formulas/f_tag.php -- HotCRP helper class for formula expressions
// Copyright (c) 2009-2024 Eddie Kohler; see LICENSE.
// Copyright (c) 2009-2025 Eddie Kohler; see LICENSE.

class Tag_Fexpr extends Fexpr {
private $tag;
Expand All @@ -20,9 +20,8 @@ static function parse_modifier(FormulaCall $ff, $arg) {
} else if (count($ff->rawargs) === 1 && $arg[0] === ":") {
$ff->rawargs[0] .= $arg;
return true;
} else {
return false;
}
return false;
}
static function make(FormulaCall $ff) {
if (count($ff->rawargs) === 1
Expand Down
19 changes: 9 additions & 10 deletions src/formulas/f_topic.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
// formulas/f_topic.php -- HotCRP helper class for formula expressions
// Copyright (c) 2009-2024 Eddie Kohler; see LICENSE.
// Copyright (c) 2009-2025 Eddie Kohler; see LICENSE.

class Topic_Fexpr extends Fexpr {
/** @var true|array<int> */
Expand All @@ -17,17 +17,16 @@ function __construct(FormulaCall $ff, Formula $formula) {
}
}
}
static function parse_modifier(FormulaCall $ff, $arg, $rest, Formula $formula) {
if ($ff->modifier === null && !str_starts_with($arg, ".")) {
if (str_starts_with($arg, ":")) {
$arg = substr($arg, 1);
}
$ff->modifier = $formula->conf->topic_set()->find_all(SearchWord::unquote($arg));
// XXX warn if no match
return true;
} else {
static function parse_modifier(FormulaCall $ff, $arg, Formula $formula) {
if ($ff->modifier !== null || str_starts_with($arg, ".")) {
return false;
}
if (str_starts_with($arg, ":")) {
$arg = substr($arg, 1);
}
$ff->modifier = $formula->conf->topic_set()->find_all(SearchWord::unquote($arg));
// XXX warn if no match
return true;
}
function paper_options(&$oids) {
$oids[PaperOption::TOPICSID] = true;
Expand Down
4 changes: 2 additions & 2 deletions src/options/o_numeric.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
// o_numeric.php -- HotCRP helper class for whole-number options
// Copyright (c) 2006-2023 Eddie Kohler; see LICENSE.
// Copyright (c) 2006-2025 Eddie Kohler; see LICENSE.

class Numeric_PaperOption extends PaperOption {
function __construct(Conf $conf, $args) {
Expand Down Expand Up @@ -80,7 +80,7 @@ function value_script_expression() {
return ["type" => "numeric", "formid" => $this->formid];
}

function parse_fexpr(FormulaCall $fcall, &$t) {
function parse_fexpr(FormulaCall $fcall) {
$fex = new OptionValue_Fexpr($this);
$fex->set_format(Fexpr::FNUMERIC);
return $fex;
Expand Down
4 changes: 2 additions & 2 deletions src/options/o_realnumber.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
// o_realnumber.php -- HotCRP helper class for whole-number options
// Copyright (c) 2006-2023 Eddie Kohler; see LICENSE.
// Copyright (c) 2006-2025 Eddie Kohler; see LICENSE.

class RealNumber_PaperOption extends PaperOption {
function __construct(Conf $conf, $args) {
Expand Down Expand Up @@ -94,7 +94,7 @@ function value_script_expression() {
return ["type" => "numeric", "formid" => $this->formid];
}

function parse_fexpr(FormulaCall $fcall, &$t) {
function parse_fexpr(FormulaCall $fcall) {
return new RealNumberOption_Fexpr($this);
}

Expand Down
9 changes: 4 additions & 5 deletions src/paperoption.php
Original file line number Diff line number Diff line change
Expand Up @@ -1025,9 +1025,8 @@ function match_script_expression($values) {
}
}

/** @param string &$t
* @return ?Fexpr */
function parse_fexpr(FormulaCall $fcall, &$t) {
/** @return ?Fexpr */
function parse_fexpr(FormulaCall $fcall) {
return null;
}
/** @return OptionPresent_Fexpr */
Expand Down Expand Up @@ -1146,7 +1145,7 @@ function value_script_expression() {
return $this->present_script_expression();
}

function parse_fexpr(FormulaCall $fcall, &$t) {
function parse_fexpr(FormulaCall $fcall) {
return $this->present_fexpr();
}
}
Expand Down Expand Up @@ -1361,7 +1360,7 @@ function value_script_expression() {
return $this->present_script_expression();
}

function parse_fexpr(FormulaCall $fcall, &$t) {
function parse_fexpr(FormulaCall $fcall) {
$fex = new OptionValue_Fexpr($this);
$fex->set_format(Fexpr::FSUBFIELD, $this);
return $fex;
Expand Down

0 comments on commit 1c9c571

Please sign in to comment.