Commit Graph

30 Commits

Author SHA1 Message Date
Torin Sandall f638d624b1 format: Fix formatter to start line after writing comments
The formatter was not starting a new line after writing comments that
preceed an expression. As a result the first part of the expression
written (e.g., "not", function name, "some", etc.) would be written
without any indentation.

Fixes #1560

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-08-22 14:48:35 -04:00
Torin Sandall 949921c8ad format: Update formatter to preserve rule assigmemnts
Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-08-07 14:22:45 -04:00
Torin Sandall 3263f54a74 ast: Rename 'var' to 'some'
This commit renames the 'var' keyword to 'some'. 'some' is more
descriptive than 'var' and will better complement an 'every' or
'forall' keyword representing for universal quantifiers.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-05-18 21:27:32 -07:00
Torin Sandall b29b9ec85b format: Update to support var keyword
Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-05-18 21:27:32 -07:00
Torin Sandall 95505b36c4 Fix formatting of empty sets
Empty sets were being printed as {} which parses as an object.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-11-19 14:53:48 -08:00
Torin Sandall a54df77662 Fix formatting of trailing comments in composites
Previously trailing comments inside arrays, objects, and sets were not
being formatted correctly. For example:

[
    1,
    2,
    # foo
]

Would result in:

[
    1,
    2,
 # foo ]

The problem was that when the sequence was ended, the comments were not
being emitted. As a result when the comments were finally emitted, the
indenting was wrong and the state of the formatter was not consistent
(and so the closing bracket appeared on the same line the comment.)

These changes modify the formatter to emit the comments when ending the
sequence, as that's the point where the indenting state is known.

Also, as part of these changes, the fix for extra newlines (#1032) has
been modified. Instead of changing the startLine and endLine behaviours
(which are a bit sensitive) we just squash trailing newlines at the end
of the formatting process.

Fixes #1060

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-11-19 14:53:48 -08:00
Kim Christensen 74b36fb1fb Only write one trailing newline at end of file
opa fmt should only add one newline at the end of the file

Fixes #1032

Signed-off-by: Kim Christensen <kimworking@gmail.com>
2018-10-24 21:51:37 -07:00
Torin Sandall e04365e6ea Update format package to tolerate nil locations
Previously, the format package would return an error if any of the AST
nodes under the input were missing a location value. When the format
package was first implemented, the main use case was formatting policies
that people had written manually--which means they are provided to OPA
as files/raw strings. As a result, it made sense to treat a missing
location as an error condition because it simplifies the formatting
implementation.

However, when policies are generated (e.g., by partial evaluation) the
AST nodes do not typically carry locations. As a result, these AST nodes
cannot be formatted nicely.

These changes modify the format package to tolerate nil location values.
If a nil location value is encountered, the format package will set the
location value on the AST node to a default location, currently row 1
column 1 with text from the AST node's string representation.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-08-29 18:46:40 -07:00
Torin Sandall ab587d008b Improve formatting of empty ast.Body
Previously, if an empty ast.Body was passed to the formatting package,
it would trigger a panic because the location getter would try to index
into an empty slice.

These changes make the location getter tolerate empty bodies and the
format package tolerate nil locations on empty bodies. The changes also
improve simplify the error message when nil locations are found.

Fixes #909

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-08-29 13:12:16 -07:00
Stephan Renatus 2f1526c672 fix misspell
Signed-off-by: Stephan Renatus <srenatus@chef.io>
2018-06-05 09:50:13 -07:00
Torin Sandall 4d29163b94 Do not elide rule head value of true in some cases
If the rule defines a constant value or is part of an incremental
definition, then the value must not be elided because that would result
in a syntax error or change the meaning of the rule.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2018-03-20 18:13:00 -07:00
Torin Sandall 4f8ea30c25 Fix format for calls with empty args 2018-03-12 13:43:51 -07:00
Torin Sandall 082e445ec9 Refactor callers to support calls as values 2018-01-26 18:05:05 -08:00
Torin Sandall 61823a2920 Refactor set and object types in ast package
Previously, sets and objects were not interfaces and as a result,
callers were relying on the underlying structure for operations such as
iteration.

These changes refactor the ast package to expose sets and objects as
interfaces so that we can change the underlying data structures without
affecting callers.
2017-12-15 09:16:34 -08:00
Torin Sandall 3d9db07da2 Fix formatting of else statements with functions
The formatter was not handling else statements for functions properly.
The function args were being written after the else keyword which
resulted in rules that did not parse.
2017-12-11 15:54:18 -08:00
Torin Sandall c84f343320 Fix bug in expression formatting
Call expressions written with infix notation were being incorrectly
formatted using the logic for arithmetic operations.
2017-11-15 09:53:18 -08:00
Torin Sandall 7ca542adb5 Refactor functions implementation
Previously, functions were implemented with a separate set of types that
had their own code paths in the compiler, eval, etc. These changes
refactor the function implementation so that functions are implemented
as rules with one or more arguments.

By representing functions as rules, we can avoid special casing required
to support functions, e.g., during parse and compile there are a number
of steps that required special casing for functions:

- Parser needed separate grammar definitions for functions (which
  prevented them from being chained or using else)

- Compiler needed separate resolver and type checker implementations
  which was a source of bugs.

In some cases, special casing is unavoidable for now (e.g., during eval)
however this could be improved in the future.

Fixes #471
Fixes #467
Fixes #463
2017-10-10 08:57:58 -07:00
Torin Sandall ea2ea9b12b Modify AST to represent function names as refs
These changes update the AST to represent function names as refs.
Previously, function names were represented as strings. Representing the
names as strings was fine, however, once functions and rules are
merged, it will be desirable to refer to functions using references.
This is a bit of preemptive refactoring to make that change easier.
Instead of having functions referred to with both strings and
references, all functions will be referred to with references.
2017-10-10 08:57:58 -07:00
Torin Sandall 7779915431 Remove use of sprintf in formatter
Do not format strings before writing them to output. String formatting
was only being used for concatenation and caused problems for Rego
strings that contain printf verbs.
2017-09-14 09:40:07 -07:00
Torin Sandall 7dec5e062c Remove panic workaround in parser
The new version of mna/pigeon fixes the location bug that prevented
accurate line/column numbers from being reported on unmatched input.

In the future we can investigate improvements to the error messages by
adding negative matches that return higher-level errors as long as the
location information is not impacted.

Fixes #214
2017-08-29 08:59:12 -07:00
Torin Sandall 588cc82f11 Add support for partial doc shorthand
These changes allow partial docs to be defined without a body in Rego
source files. Before, the rules would have to include a `{true}` body
for the parser to allow them. Now, the body can be omitted.

Rules defined this way (inside modules) cannot be copy/pasted as-is into
the REPL. This could be addressed by creating a "paste mode" in the REPL
similar to ipython and other interactive shells.

These changes build on https://github.com/open-policy-agent/opa/pull/412
with a few differences:

- Dynamic values are allowed in the head.
- Partial sets are allowed.

Both of these changes are based on personal experience writing policy.
Dynamic values are fine to allow as the compiler will catch unsafe vars
and rewrite the head to handle refs and comprehensions.
2017-08-24 11:29:14 -07:00
Torin Sandall c0c0cd587d Add godoc package description for format 2017-07-20 19:31:10 -07:00
Matthew Mussomele 62644a63bd Update format package to support raw strings 2017-07-20 15:59:11 -07:00
Matthew Mussomele 80ca430ee2 Properly format lone rule heads
Format was just outputting the string representation of rule heads
passed to Ast, but they should be formatted like they are when part of
rules.
2017-07-12 08:11:23 -07:00
Matthew Mussomele afee74be1e Update format package with object and set comprehensions 2017-07-12 08:11:23 -07:00
Matthew Mussomele 326cb91298 Update format to support user functions
This patch also fixes a bug in format which would cause comments
to be printed after a function or rule if they had an object or
set literal in their input set.

Objects and sets use curly braces to group their elements, and so format
mistook the bounds of the object or set inputs as the bounds of the
function/rule body (which also uses curly braces to group). This caused
the discovered closing location of rules and functions to be on an
earlier line than in reality, causing comments at the end to be written
after the body, not inside it.

This has been fixed, as the heads of functions and rules are skipped
before scanning for their closing braces.
2017-07-05 13:45:55 -07:00
Matthew Mussomele 45af66c63b Correct OPA fmt to leave policy constants as Bodies
When parsing, OPA transforms constants into rules with only the
expression `true` in the body. This is not desirable behavior, as
it's much cleaner for them to be expressed in the shorthand.

This patch also adds a test for parsing invalid rego.
2017-06-30 10:51:10 -04:00
Matthew Mussomele 255be29ec0 Correct formatting of zero length iterables
The format package incorrectly assumed that iterables would have
positive length, resulting in an indexing error.
2017-06-30 10:51:10 -04:00
Matthew Mussomele c30c5be5c6 Include filename in formatting error messages 2017-06-30 10:51:10 -04:00
Matthew Mussomele af029e0150 Implement formatting package for Rego
This patch adds a small formatting package to OPA that is capable of
formatting Rego source code.
2017-06-29 13:10:36 -04:00