Steven Haryanto > Data-Sah > Data::Sah::Compiler::Prog

Download:
Data-Sah-0.29.tar.gz

Dependencies

Annotate this POD

Website

CPAN RT

Open  0
View/Report Bugs
Module Version: 0.29   Source  

NAME ^

Data::Sah::Compiler::Prog - Base class for programming language compilers

VERSION ^

This document describes version 0.29 of Data::Sah::Compiler::Prog (from Perl distribution Data-Sah), released on 2014-06-30.

SYNOPSIS ^

DESCRIPTION ^

This class is derived from Data::Sah::Compiler. It is used as base class for compilers which compile schemas into code (validator) in several programming languages, Perl (Data::Sah::Compiler::perl) and JavaScript (Data::Sah::Compiler::js) being two of them. (Other similar programming languages like PHP and Ruby might also be supported later on if needed).

Compilers using this base class are flexible in the kind of code they produce:

HOW IT WORKS ^

The compiler generates code in the following form:

 EXPR && EXPR2 && ...

where EXPR can be a single expression or multiple expressions joined by the list operator (which Perl and JavaScript support). Each EXPR is typically generated out of a single schema clause. Some pseudo-example of generated JavaScript code:

 (data >= 0)  # from clause: min => 0
 &&
 (data <= 10) # from clause: max => 10

Another example, a fuller translation of schema [int => {min=>0, max=>10}] to Perl, returning string result (error message) instead of boolean:

 # from clause: req => 0
 !defined($data) ? 1 : (

     # type check
     ($data =~ /^[+-]?\d+$/ ? 1 : ($err //= "Data is not an integer", 0))

     &&

     # from clause: min => 0
     ($data >=  0 ? 1 : ($err //= "Must be at least 0", 0))

     &&

     # from clause: max => 10
     ($data <= 10 ? 1 : ($err //= "Must be at most 10", 0))

 )

The final validator code will add enclosing subroutine and variable declaration, loading of modules, etc.

Note: Current assumptions/hard-coded things for the supported languages: ternary operator (? :), semicolon as statement separator.

ATTRIBUTES ^

These usually need not be set/changed by users.

hc => OBJ

Instance of Data::Sah::Compiler::human, to generate error messages.

comment_style => STR

Specify how comments are written in the target language. Either 'cpp' (// comment), 'shell' (# comment), 'c' (/* comment */), or 'ini' (; comment). Each programming language subclass will set this, for example, the perl compiler sets this to 'shell' while js sets this to 'cpp'.

var_sigil => STR

concat_op => STR

logical_and_op => STR

logical_not_op => STR

METHODS ^

new() => OBJ

$c->compile(%args) => RESULT

Aside from base class' arguments, this class supports these arguments (suffix * denotes required argument):

Compilation data

This subclass adds the following compilation data ($cd).

Keys which contain compilation state:

Keys which contain compilation result:

$c->comment($cd, @args) => STR

Generate a comment. For example, in perl compiler:

 $c->comment($cd, "123"); # -> "# 123\n"

Will return an empty string if compile argument comment is set to false.

INTERNAL VARIABLES IN THE GENERATED CODE ^

The generated code maintains the following variables. _sahv_ prefix stands for "Sah validator", it is used to minimize clash with data_term.

HOMEPAGE ^

Please visit the project's homepage at https://metacpan.org/release/Data-Sah.

SOURCE ^

Source repository is at https://github.com/sharyanto/perl-Data-Sah.

BUGS ^

Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=Data-Sah

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.

AUTHOR ^

Steven Haryanto <stevenharyanto@gmail.com>

COPYRIGHT AND LICENSE ^

This software is copyright (c) 2014 by Steven Haryanto.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

syntax highlighting: