Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r75627 - in branches/quickbook-dev/tools/quickbook: src test/snippets
From: dnljms_at_[hidden]
Date: 2011-11-22 18:49:28


Author: danieljames
Date: 2011-11-22 18:49:26 EST (Tue, 22 Nov 2011)
New Revision: 75627
URL: http://svn.boost.org/trac/boost/changeset/75627

Log:
Quickbook: Deal with unbalanced code snippets.
Added:
   branches/quickbook-dev/tools/quickbook/test/snippets/unbalanced_snippet1-1_5.gold (contents, props changed)
   branches/quickbook-dev/tools/quickbook/test/snippets/unbalanced_snippet1-1_5.quickbook (contents, props changed)
   branches/quickbook-dev/tools/quickbook/test/snippets/unbalanced_snippet1-1_6-fail.quickbook (contents, props changed)
   branches/quickbook-dev/tools/quickbook/test/snippets/unbalanced_snippet1.cpp (contents, props changed)
   branches/quickbook-dev/tools/quickbook/test/snippets/unbalanced_snippet2-1_6-fail.quickbook (contents, props changed)
   branches/quickbook-dev/tools/quickbook/test/snippets/unbalanced_snippet2.cpp (contents, props changed)
Text files modified:
   branches/quickbook-dev/tools/quickbook/src/code_snippet.cpp | 57 +++++++++++++++++++++++++++++++++++----
   branches/quickbook-dev/tools/quickbook/src/files.cpp | 43 ++++++++++++++++--------------
   branches/quickbook-dev/tools/quickbook/test/snippets/Jamfile.v2 | 3 ++
   3 files changed, 76 insertions(+), 27 deletions(-)

Modified: branches/quickbook-dev/tools/quickbook/src/code_snippet.cpp
==============================================================================
--- branches/quickbook-dev/tools/quickbook/src/code_snippet.cpp (original)
+++ branches/quickbook-dev/tools/quickbook/src/code_snippet.cpp 2011-11-22 18:49:26 EST (Tue, 22 Nov 2011)
@@ -18,6 +18,7 @@
 #include "actions.hpp"
 #include "values.hpp"
 #include "files.hpp"
+#include "input_path.hpp"
 
 namespace quickbook
 {
@@ -35,6 +36,7 @@
             , storage(storage)
             , source_file(source_file)
             , source_type(source_type)
+ , error_count(0)
         {
             content.start(source_file);
         }
@@ -47,6 +49,7 @@
         void end_snippet(string_iterator first, string_iterator last);
         void end_snippet_impl(string_iterator);
         void callout(string_iterator first, string_iterator last);
+ void end_file(string_iterator, string_iterator);
         
         void append_code(string_iterator first, string_iterator last);
         void close_code();
@@ -97,6 +100,7 @@
         std::vector<template_symbol>& storage;
         file_ptr source_file;
         char const* const source_type;
+ int error_count;
     };
 
     struct python_code_snippet_grammar
@@ -118,7 +122,8 @@
 
                 actions_type& actions = self.actions;
             
- start_ = *code_elements;
+ start_ = (*code_elements) [boost::bind(&actions_type::end_file, &actions, _1, _2)]
+ ;
 
                 identifier =
                     (cl::alpha_p | '_') >> *(cl::alnum_p | '_')
@@ -222,7 +227,8 @@
             {
                 actions_type& actions = self.actions;
             
- start_ = *code_elements;
+ start_ = (*code_elements) [boost::bind(&actions_type::end_file, &actions, _1, _2)]
+ ;
 
                 identifier =
                     (cl::alpha_p | '_') >> *(cl::alnum_p | '_')
@@ -383,8 +389,7 @@
         }
 
         assert(info.full);
-
- return 0;
+ return a.error_count;
     }
 
     void code_snippet_actions::append_code(string_iterator first, string_iterator last)
@@ -408,7 +413,7 @@
         
         last_code_pos = last;
     }
-
+
     void code_snippet_actions::close_code()
     {
         if (!snippet_stack) return;
@@ -495,11 +500,47 @@
 
     void code_snippet_actions::end_snippet(string_iterator first, string_iterator last)
     {
- // TODO: Error?
- if(!snippet_stack) return;
         append_code(first, last);
+
+ if(!snippet_stack) {
+ if (qbk_version_n >= 106u) {
+ detail::outerr(source_file, first)
+ << "Mismatched end snippet."
+ << std::endl;
+ ++error_count;
+ }
+ else {
+ detail::outwarn(source_file, first)
+ << "Mismatched end snippet."
+ << std::endl;
+ }
+ return;
+ }
+
         end_snippet_impl(first);
     }
+
+ void code_snippet_actions::end_file(string_iterator, string_iterator pos)
+ {
+ append_code(pos, pos);
+ close_code();
+
+ while (snippet_stack) {
+ if (qbk_version_n >= 106u) {
+ detail::outerr(source_file->path)
+ << "Unclosed snippet '" << snippet_stack->id << "'"
+ << std::endl;
+ ++error_count;
+ }
+ else {
+ detail::outwarn(source_file->path)
+ << "Unclosed snippet '" << snippet_stack->id << "'"
+ << std::endl;
+ }
+
+ end_snippet_impl(pos);
+ }
+ }
 
     void code_snippet_actions::start_snippet_impl(std::string const& id,
             string_iterator position)
@@ -509,6 +550,8 @@
 
     void code_snippet_actions::end_snippet_impl(string_iterator position)
     {
+ assert(snippet_stack);
+
         boost::shared_ptr<snippet_data> snippet = pop_snippet_data();
         value callouts = snippet->callouts.release();
 

Modified: branches/quickbook-dev/tools/quickbook/src/files.cpp
==============================================================================
--- branches/quickbook-dev/tools/quickbook/src/files.cpp (original)
+++ branches/quickbook-dev/tools/quickbook/src/files.cpp 2011-11-22 18:49:26 EST (Tue, 22 Nov 2011)
@@ -394,29 +394,32 @@
         assert(begin <= x.data->new_file->source.size());
         assert(end <= x.data->new_file->source.size());
 
- std::vector<mapped_file_section>::iterator start =
- boost::upper_bound(x.data->new_file->mapped_sections,
- begin, mapped_section_pos_cmp());
- assert(start != x.data->new_file->mapped_sections.begin());
- --start;
-
- std::string::size_type size = data->new_file->source.size();
-
- data->new_file->mapped_sections.push_back(mapped_file_section(
- start->to_original_pos(begin), size,
- start->section_type));
-
- for (++start; start != x.data->new_file->mapped_sections.end() &&
- start->our_pos < end; ++start)
+ if (begin != end)
         {
+ std::vector<mapped_file_section>::iterator start =
+ boost::upper_bound(x.data->new_file->mapped_sections,
+ begin, mapped_section_pos_cmp());
+ assert(start != x.data->new_file->mapped_sections.begin());
+ --start;
+
+ std::string::size_type size = data->new_file->source.size();
+
             data->new_file->mapped_sections.push_back(mapped_file_section(
- start->original_pos, start->our_pos - begin + size,
- start->section_type));
- }
-
- data->new_file->source.append(
- x.data->new_file->source.begin() + begin,
+ start->to_original_pos(begin), size,
+ start->section_type));
+
+ for (++start; start != x.data->new_file->mapped_sections.end() &&
+ start->our_pos < end; ++start)
+ {
+ data->new_file->mapped_sections.push_back(mapped_file_section(
+ start->original_pos, start->our_pos - begin + size,
+ start->section_type));
+ }
+
+ data->new_file->source.append(
+ x.data->new_file->source.begin() + begin,
             x.data->new_file->source.begin() + end);
+ }
     }
 
     void mapped_file_builder::unindent_and_add(iterator begin, iterator end)

Modified: branches/quickbook-dev/tools/quickbook/test/snippets/Jamfile.v2
==============================================================================
--- branches/quickbook-dev/tools/quickbook/test/snippets/Jamfile.v2 (original)
+++ branches/quickbook-dev/tools/quickbook/test/snippets/Jamfile.v2 2011-11-22 18:49:26 EST (Tue, 22 Nov 2011)
@@ -12,4 +12,7 @@
 
 test-suite quickbook.test :
     [ quickbook-test pass_thru ]
+ [ quickbook-test unbalanced_snippet1-1_5 ]
+ [ quickbook-error-test unbalanced_snippet1-1_6-fail ]
+ [ quickbook-error-test unbalanced_snippet2-1_6-fail ]
     ;

Added: branches/quickbook-dev/tools/quickbook/test/snippets/unbalanced_snippet1-1_5.gold
==============================================================================
--- (empty file)
+++ branches/quickbook-dev/tools/quickbook/test/snippets/unbalanced_snippet1-1_5.gold 2011-11-22 18:49:26 EST (Tue, 22 Nov 2011)
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE article PUBLIC "-//Boost//DTD BoostBook XML V1.0//EN" "http://www.boost.org/tools/boostbook/dtd/boostbook.dtd">
+<article id="unbalanced_snippet_fail_test_1" last-revision="DEBUG MODE Date: 2000/12/20 12:00:00 $"
+ xmlns:xi="http://www.w3.org/2001/XInclude">
+ <title>Unbalanced snippet fail test 1</title>
+ <para>
+<programlisting><phrase role="keyword">int</phrase> <phrase role="identifier">main</phrase><phrase role="special">()</phrase> <phrase role="special">{}</phrase></programlisting>
+ </para>
+</article>

Added: branches/quickbook-dev/tools/quickbook/test/snippets/unbalanced_snippet1-1_5.quickbook
==============================================================================
--- (empty file)
+++ branches/quickbook-dev/tools/quickbook/test/snippets/unbalanced_snippet1-1_5.quickbook 2011-11-22 18:49:26 EST (Tue, 22 Nov 2011)
@@ -0,0 +1,8 @@
+[article Unbalanced snippet fail test 1
+[quickbook 1.5]
+]
+
+[import unbalanced_snippet1.cpp]
+[import unbalanced_snippet2.cpp]
+
+[unclosed]
\ No newline at end of file

Added: branches/quickbook-dev/tools/quickbook/test/snippets/unbalanced_snippet1-1_6-fail.quickbook
==============================================================================
--- (empty file)
+++ branches/quickbook-dev/tools/quickbook/test/snippets/unbalanced_snippet1-1_6-fail.quickbook 2011-11-22 18:49:26 EST (Tue, 22 Nov 2011)
@@ -0,0 +1,7 @@
+[article Unbalanced snippet fail test 1
+[quickbook 1.6]
+]
+
+[import unbalanced_snippet1.cpp]
+
+[unclosed]
\ No newline at end of file

Added: branches/quickbook-dev/tools/quickbook/test/snippets/unbalanced_snippet1.cpp
==============================================================================
--- (empty file)
+++ branches/quickbook-dev/tools/quickbook/test/snippets/unbalanced_snippet1.cpp 2011-11-22 18:49:26 EST (Tue, 22 Nov 2011)
@@ -0,0 +1,3 @@
+//[unclosed
+
+int main() {}
\ No newline at end of file

Added: branches/quickbook-dev/tools/quickbook/test/snippets/unbalanced_snippet2-1_6-fail.quickbook
==============================================================================
--- (empty file)
+++ branches/quickbook-dev/tools/quickbook/test/snippets/unbalanced_snippet2-1_6-fail.quickbook 2011-11-22 18:49:26 EST (Tue, 22 Nov 2011)
@@ -0,0 +1,5 @@
+[article Unbalanced snippet fail test 2
+[quickbook 1.6]
+]
+
+[import unbalanced_snippet2.cpp]

Added: branches/quickbook-dev/tools/quickbook/test/snippets/unbalanced_snippet2.cpp
==============================================================================
--- (empty file)
+++ branches/quickbook-dev/tools/quickbook/test/snippets/unbalanced_snippet2.cpp 2011-11-22 18:49:26 EST (Tue, 22 Nov 2011)
@@ -0,0 +1 @@
+//]
\ No newline at end of file


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk