text:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

	<modelVersion>4.0.0</modelVersion>

	<groupId>com.c4soft.hadoop2</groupId>
	<artifactId>parent</artifactId>
	<version>1.0.0</version>
	<packaging>pom</packaging>

	<organization>
		<name>C4</name>
	</organization>
	<developers>
		<developer>
			<email>ch4mp@c4-soft.com</email>
			<name>Ch4mp</name>
			<organization>C4</organization>
		</developer>
	</developers>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<junit.version>4.10</junit.version>
		<java.version>1.8</java.version>
		<hadoop.classifier>hadoop2</hadoop.classifier>
		<hadoop2.version>2.6.0</hadoop2.version>
		<spring.hadoop.version>2.0.4.RELEASE-hadoop26</spring.hadoop.version>
		<avro.version>1.7.7</avro.version>
		<pig.version>0.14.0</pig.version>
	</properties>

	<modules>
		<module>labs</module>
		<module>test-util</module>
		<module>util</module>
	</modules>

	<repositories>
		<!-- repository> <id>cloudera</id> <url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
			</repository -->
		<repository>
			<id>spring-milestone</id>
			<name>Spring Maven SNAPSHOT Repository</name>
			<url>http://repo.springsource.org/milestone/</url>
		</repository>
		<repository>
			<id>apache-repo-snapshots</id>
			<url>https://repository.apache.org/content/repositories/snapshots/</url>
			<releases>
				<enabled>false</enabled>
			</releases>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
		</repository>
	</repositories>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>commons-io</groupId>
				<artifactId>commons-io</artifactId>
				<version>2.4</version>
			</dependency>
			<dependency>
				<groupId>junit</groupId>
				<artifactId>junit</artifactId>
				<version>${junit.version}</version>
				<scope>test</scope>
			</dependency>
			<dependency>
				<groupId>org.apache.avro</groupId>
				<artifactId>avro</artifactId>
				<version>${avro.version}</version>
			</dependency>
			<dependency>
				<groupId>org.apache.avro</groupId>
				<artifactId>avro-mapred</artifactId>
				<version>${avro.version}</version>
				<classifier>${hadoop.classifier}</classifier>
			</dependency>
			<dependency>
				<groupId>org.apache.hadoop</groupId>
				<artifactId>hadoop-client</artifactId>
				<version>${hadoop2.version}</version>
			</dependency>
			<dependency>
				<groupId>org.apache.hadoop</groupId>
				<artifactId>hadoop-minicluster</artifactId>
				<version>${hadoop2.version}</version>
				<scope>test</scope>
			</dependency>
			<dependency>
				<groupId>org.apache.mrunit</groupId>
				<artifactId>mrunit</artifactId>
				<version>1.1.0</version>
				<classifier>${hadoop.classifier}</classifier>
				<scope>test</scope>
			</dependency>
			<dependency>
				<groupId>org.apache.pig</groupId>
				<artifactId>pig</artifactId>
				<version>${pig.version}</version>
			</dependency>
			<dependency>
				<groupId>org.apache.pig</groupId>
				<artifactId>pigunit</artifactId>
				<version>${pig.version}</version>
				<scope>test</scope>
			</dependency>
			<dependency>
				<groupId>org.springframework.data</groupId>
				<artifactId>spring-data-hadoop</artifactId>
				<version>${spring.hadoop.version}</version>
			</dependency>
			<dependency>
				<groupId>com.googlecode.json-simple</groupId>
				<artifactId>json-simple</artifactId>
				<version>1.1.1</version>
			</dependency>
			<dependency>
				<groupId>org.xerial.snappy</groupId>
				<artifactId>snappy-java</artifactId>
				<version>1.1.1.6</version>
			</dependency>
			<dependency>
				<groupId>com.c4soft.hadoop2</groupId>
				<artifactId>util</artifactId>
				<version>${project.version}</version>
			</dependency>
			<dependency>
				<groupId>com.c4soft.hadoop2</groupId>
				<artifactId>test-util</artifactId>
				<version>${project.version}</version>
				<scope>test</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
		</dependency>
		<dependency>
			<groupId>org.apache.mrunit</groupId>
			<artifactId>mrunit</artifactId>
			<classifier>${hadoop.classifier}</classifier>
		</dependency>
	</dependencies>

	<build>
		<pluginManagement>
			<plugins>
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-compiler-plugin</artifactId>
					<version>3.2</version>
					<configuration>
						<verbose>true</verbose>
						<compilerVersion>${java.version}</compilerVersion>
						<source>${java.version}</source>
						<target>${java.version}</target>
						<showWarnings>true</showWarnings>
					</configuration>
				</plugin>
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-surefire-plugin</artifactId>
					<version>2.18.1</version>
				</plugin>
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-resources-plugin</artifactId>
					<version>2.7</version>
					<configuration>
						<encoding>${project.build.sourceEncoding}</encoding>
					</configuration>
				</plugin>
				<plugin>
					<groupId>org.apache.avro</groupId>
					<artifactId>avro-maven-plugin</artifactId>
					<version>${avro.version}</version>
					<executions>
						<execution>
							<phase>generate-sources</phase>
							<goals>
								<goal>schema</goal>
							</goals>
							<configuration>
								<sourceDirectory>${project.basedir}/src/main/avro/</sourceDirectory>
								<outputDirectory>${project.basedir}/target/generated-sources/</outputDirectory>
							</configuration>
						</execution>
					</executions>
				</plugin>
				<plugin>
					<groupId>org.eclipse.m2e</groupId>
					<artifactId>lifecycle-mapping</artifactId>
					<version>1.0.0</version>
					<configuration>
						<lifecycleMappingMetadata>
							<pluginExecutions>
								<pluginExecution>
									<pluginExecutionFilter>
										<groupId>org.apache.avro</groupId>
										<artifactId>avro-maven-plugin</artifactId>
										<versionRange>${avro.version}</versionRange>
										<goals>
											<goal>schema</goal>
										</goals>
									</pluginExecutionFilter>
									<action>
										<execute>
											<runOnIncremental>true</runOnIncremental>
										</execute>
									</action>
								</pluginExecution>
							</pluginExecutions>
						</lifecycleMappingMetadata>
					</configuration>
				</plugin>
			</plugins>
		</pluginManagement>

		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-resources-plugin</artifactId>
			</plugin>
		</plugins>

		<resources>
			<resource>
				<directory>src/main/resources</directory>
			</resource>
			<resource>
				<directory>src/main/pig</directory>
			</resource>
		</resources>

		<testResources>
			<testResource>
				<directory>src/test/resources</directory>
			</testResource>
			<testResource>
				<directory>src/main/pig</directory>
			</testResource>
		</testResources>
	</build>
</project>


text:
'use strict';

/**
 * Module dependencies.
 */
var mongoose    = require('mongoose'),
    Event       = mongoose.model('Event'),
    _           = require('lodash');

/**
 * Find event by id
 */
exports.event = function(req, res, next, id) {
    Event.load(id, function(err, event) {
        if (err) return next(err);
        if (!event) return next(new Error('Failed to load event ' + id));
        req.event = event;
        next();
    });
};

/**
 * Create Event
 */
exports.create = function(req, res) {
    var event = new Event(req.body);
    console.log(req.body);
    event.save(function(err) {
        var errorMsg = '';
        if (err) {
            switch(err.code){
                case 11000:
                case 11001:
                    errorMsg = 'Title already exsits.';
                    break;
                default:
                    errorMsg ='Error occured while processing your request.';
            }
            console.log(err);
            return res.status(400).send(errorMsg);

        }
        res.jsonp(event);
    });
};

/**
 * Update an Event
 */
exports.update = function(req, res) {
    var event = req.event;

    event = _.extend(event, req.body);

    event.save(function(err) {
        var errorMsg = '';
        if (err) {
            switch(err.code){
                case 11000:
                case 11001:
                    errorMsg = 'Title already exsits.';
                    break;
                default:
                    errorMsg ='Error occured while processing your request.';
            }
            return res.status(400).send(errorMsg);
        }
        res.jsonp(event);
    });
};
/**
 * Show an event
 */
exports.show = function(req, res) {
    res.jsonp(req.event);
};

/**
 * List of Events
 */
exports.all = function(req, res) {
    Event.find().exec(function(err, event) {
        if (err) {
            return res.jsonp(500, {
                error: 'Cannot list the event'
            });
        }
        res.jsonp(event);
    });
};

redpajama_set_name: RedPajamaGithub


text:
module Mail
  class Message

    include Chimpster::SharedMessageExtensions

    def html?
      content_type && content_type.include?('text/html')
    end

    def body_html
      if html_part.nil?
        body.to_s if html?
      else
        html_part.body.to_s
      end
    end

    def body_text
      if text_part.nil?
        body.to_s
      else
        text_part.body.to_s
      end
    end

  end
end


text:
module NUCore

  class PermissionDenied < RuntimeError
  end

  class Error < StandardError
  end

  class MixedFacilityCart < NUCore::Error
  end

  class NotPermittedWhileActingAs < NUCore::Error
  end

  class PurchaseException < NUCore::Error; end

end


text:
import { module, test } from 'qunit';
/* eslint-disable camelcase */

import { setupRenderingTest } from 'ember-qunit';
import { render } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';

module('twitter-entity/media', function(hooks) {
  setupRenderingTest(hooks);

  test('it renders', async function(assert) {
    assert.expect(1);

    this.set('mediaEntity', {
      url: 'http://t.co/myimg',
      display_url: 'pic.twitter.com/123'
    });

    await render(hbs`{{twitter-entity/media entity=this.mediaEntity}}`);

    assert.equal(this.element.innerHTML, 'pic.twitter.com/123');
  });
});


text:
using System;
using System.Threading;
using System.Web.UI.WebControls;
using Adxstudio.Xrm;
using Adxstudio.Xrm.Blogs;
using Adxstudio.Xrm.Core.Flighting;
using Adxstudio.Xrm.Diagnostics;
using Adxstudio.Xrm.Web.Mvc;
using Microsoft.Xrm.Portal;
using Microsoft.Xrm.Portal.Configuration;
using Site.Pages;

namespace Site.Areas.Blogs.Pages
{
	public partial class BlogPost : PortalPage
	{
		private readonly Lazy<IPortalContext> _portal = new Lazy<IPortalContext>(() => PortalCrmConfigurationManager.CreatePortalContext(), LazyThreadSafetyMode.None);

		protected void Page_Load(object sender, EventArgs e)
		{
			if (IsPostBack)
			{
			    Post.DataBind();
			}
		}

		protected void CreateBlogPostDataAdapter(object sender, ObjectDataSourceEventArgs e)
		{
			e.ObjectInstance = new BlogPostDataAdapter(_portal.Value.Entity, new PortalContextDataAdapterDependencies(_portal.Value, requestContext: Request.RequestContext));

			// sprinkle these calls in for whichever events we want to trace
			//Log Customer Journey Tracking
			if (FeatureCheckHelper.IsFeatureEnabled(FeatureNames.CustomerJourneyTracking))
			{
				if (!String.IsNullOrEmpty(_portal.Value.Entity.Id.ToString()) &&
				    !String.IsNullOrEmpty(_portal.Value.Entity.GetAttributeValue<string>("adx_name")))
				{
					PortalTrackingTrace.TraceInstance.Log(Constants.Blog, _portal.Value.Entity.Id.ToString(),
						_portal.Value.Entity.GetAttributeValue<string>("adx_name"));
				}
			}
		}
	}
}


text:
package org.ibase4j.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.ibase4j.core.base.BaseService;
import org.ibase4j.core.util.CacheUtil;
import org.ibase4j.core.util.InstanceUtil;
import org.ibase4j.core.util.PropertiesUtil;
import org.ibase4j.mapper.SysSessionMapper;
import org.ibase4j.model.SysSession;

/**
 * @author ShenHuaJie
 * @version 2016年5月20日 下午3:19:19
 */
@Service
@CacheConfig(cacheNames = "sysSession")
public class SysSessionService extends BaseService<SysSession> {

	@CachePut
	@Transactional
	public SysSession update(SysSession record) {
		if (record != null && record.getId() == null) {
			record.setUpdateTime(new Date());
			Long id = ((SysSessionMapper) mapper).queryBySessionId(record.getSessionId());
			if (id != null) {
				mapper.updateById(record);
			} else {
				record.setCreateTime(new Date());
				mapper.insert(record);
			}
		} else {
			mapper.updateById(record);
		}
		return record;
	}

	// 系统触发,由系统自动管理缓存
	public void deleteBySessionId(final SysSession sysSession) {
		if (sysSession != null) {
			((SysSessionMapper) mapper).deleteBySessionId(sysSession.getSessionId());
		}
	}

	public List<String> querySessionIdByAccount(SysSession sysSession) {
		if (sysSession != null) {
			return ((SysSessionMapper) mapper).querySessionIdByAccount(sysSession.getAccount());
		}
		return null;
	}

	//
	public void cleanExpiredSessions() {
		String key = "spring:session:" + PropertiesUtil.getString("session.redis.namespace") + ":sessions:expires:";
		Map<String, Object> columnMap = InstanceUtil.newHashMap();
		List<SysSession> sessions = queryList(columnMap);
		for (SysSession sysSession : sessions) {
			if (sysSession != null) {
				logger.info("检查SESSION : {}", sysSession.getSessionId());
				if (!CacheUtil.getCache().exists(key + sysSession.getSessionId())) {
					logger.info("移除SESSION : {}", sysSession.getSessionId());
					delete(sysSession.getId());
				}
			}
		}
	}
}


text:
require_relative 'event_stream'
class GridService
  include Mongoid::Document
  include Mongoid::Timestamps
  include EventStream

  LB_IMAGE = 'kontena/lb'

  field :image_name, type: String
  field :labels, type: Hash, default: {}
  field :affinity, type: Array, default: []
  field :name, type: String
  field :stateful, type: Boolean, default: false
  field :user, type: String
  field :container_count, type: Integer, default: 1
  field :cmd, type: Array
  field :entrypoint, type: String
  field :ports, type: Array, default: []
  field :env, type: Array, default: []
  field :memory, type: Integer
  field :memory_swap, type: Integer
  field :shm_size, type: Integer
  field :cpus, type: Float
  field :cpu_shares, type: Integer
  field :volumes, type: Array, default: []
  field :volumes_from, type: Array, default: []
  field :privileged, type: Boolean
  field :cap_add, type: Array, default: []
  field :cap_drop, type: Array, default: []
  field :net, type: String, default: 'bridge'
  field :state, type: String, default: 'initialized'
  field :log_driver, type: String
  field :log_opts, type: Hash, default: {}
  field :devices, type: Array, default: []
  field :pid, type: String
  field :read_only, type: Boolean, default: false

  field :deploy_requested_at, type: DateTime
  field :deployed_at, type: DateTime
  field :revision, type: Integer, default: 1
  field :stack_revision, type: Integer
  field :strategy, type: String, default: 'ha'
  field :stop_signal, type: String
  field :stop_grace_period, type: Integer, default: 10

  belongs_to :grid
  belongs_to :image
  belongs_to :stack
  has_many :grid_service_instances, dependent: :destroy
  has_many :containers
  has_many :container_logs
  has_many :container_stats
  has_many :audit_logs
  has_many :grid_service_deploys, dependent: :delete
  has_many :event_logs
  has_many :grid_domain_authorizations
  has_and_belongs_to_many :networks
  embeds_many :grid_service_links
  embeds_many :hooks, class_name: 'GridServiceHook'
  embeds_many :secrets, class_name: 'GridServiceSecret'
  embeds_many :service_volumes, class_name: 'ServiceVolume'
  embeds_many :certificates, class_name: 'GridServiceCertificate'
  embeds_one :deploy_opts, class_name: 'GridServiceDeployOpt', autobuild: true
  embeds_one :health_check, class_name: 'GridServiceHealthCheck'

  index({ grid_id: 1 })
  index({ name: 1 })
  index({ grid_service_ids: 1 })

  validates_presence_of :name, :image_name, :grid_id, :stack_id
  validates_uniqueness_of :name, scope: [:grid_id, :stack_id]

  scope :load_balancer, -> { where(image_name: /^#{LB_IMAGE}:.+/) }

  before_validation :ensure_stack

  # @return [String]
  def to_path
    "#{self.grid.try(:name)}/#{self.stack.try(:name)}/#{self.name}"
  end

  # @return [String]
  def name_with_stack
    if default_stack?
      self.name
    else
      "#{self.stack.name}.#{self.name}"
    end
  end

  # @return [String]
  def qualified_name
    parts = []
    parts << self.stack.name unless self.default_stack?
    parts << self.name

    parts.join('/')
  end

  # @return [Boolean]
  def default_stack?
    self.stack.try(:name).to_s == Stack::NULL_STACK
  end

  # @param [Integer] instance_number
  # @return [String]
  def instance_hostname(instance_number)
    "#{self.name}-#{instance_number}"
  end

  # @return [String]
  def domain
    self.stack.domain
  end

  # @param [String] state
  def set_state(state)
    state_changed = self.state != state
    self.set(:state => state)
    publish_update_event if state_changed
  end

  # @return [Boolean]
  def stateful?
    self.stateful == true
  end

  # @return [Boolean]
  def stateless?
    !stateful?
  end

  def daemon?
    self.strategy == 'daemon'
  end

  # @return [Boolean]
  def initialized?
    self.state == 'initialized'
  end

  # @return [Boolean]
  def running?
    self.state == 'running'
  end

  # @return [Boolean]
  def stopped?
    self.state == 'stopped'
  end

  # @return [Boolean]
  def stack_exposed?
    return false unless self.stack
    self.stack.exposed_service?(self)
  end

  # The service has deploys created, queued or started, but not yet finished.
  #
  # Equvialent to deploy_pending? || deploy_running?
  #
  # Ignores any expired deploys.
  #
  # @return [Boolean]
  def deploying?
    self.grid_service_deploys.deploying.count > 0
  end

  # The service has deploys created or queued, but not yet started, running or finished.
  #
  # @return [Boolean]
  def deploy_pending?
    self.grid_service_deploys.pending.count > 0
  end

  # The service has deploys that have been started, but not yet finished or timeout.
  #
  # @return [Boolean]
  def deploy_running?
    self.grid_service_deploys.running.count > 0
  end

  # @return [Boolean]
  def load_balancer?
    self.image_name.to_s.include?(LB_IMAGE) ||
      (self.env && self.env.include?('KONTENA_SERVICE_ROLE=lb'))
  end

  # @return [Boolean]
  def linked_to_load_balancer?
    self.grid_service_links.map{|l| l.linked_grid_service }.any?{|s| s.load_balancer? }
  end

  # @return [Array<GridService>]
  def linked_to_load_balancers
    self.grid_service_links.map{|l| l.linked_grid_service }.select{|s| s.load_balancer? }
  end

  # @param [GridService] service
  # @param [String] service_alias
  def link_to(service, service_alias = nil)
    service_alias = service.name if service_alias.nil?
    self.grid_service_links << GridServiceLink.new(
      linked_grid_service: service,
      alias: service_alias
    )
  end

  # @return [Hash]
  def env_hash
    if @env_hash.nil?
      @env_hash = Hash[Array(self.env).map { |kv_pair| kv_pair.split('=', 2) }]
    end

    @env_hash
  end

  # @return [Boolean]
  def overlay_network?
    self.net.to_s == 'bridge'
  end

  ##
  # @return [Container,NilClass]
  def container_by_name(name)
    self.containers.find_by(name: name.to_s)
  end

  ##
  # @return [Container,NilClass]
  def volume_by_name(name)
    self.containers.unscoped.volumes.find_by(name: name.to_s)
  end

  # @return [Mongoid::Criteria]
  def linked_from_services
    self.grid.grid_services.where(:'grid_service_links.linked_grid_service_id' => self.id)
  end

  def health_status
    healthy = 0
    unhealthy = 0
    self.containers.each do |c|
      healthy += 1 if c.health_status == 'healthy'
      unhealthy += 1 if c.health_status == 'unhealthy'
    end

    {healthy: healthy, unhealthy: unhealthy, total: self.containers.count}
  end

  def ensure_stack
    if self.grid_id && self.stack_id.nil?
      self.stack = self.grid.stacks.find_by(name: Stack::NULL_STACK)
    end
  end

  # @return [Array<String>]
  def affinity
    affinity = super
    if (affinity.nil? || affinity.empty?) && self.grid
      self.grid.default_affinity.to_a
    else
      affinity.to_a
    end
  end
end


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.OleDb;
using System.Data;
using Parser;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\..\..\infocod-oct-2013.xls;Extended Properties=Excel 8.0");
            var addresses = GetBucharest(con).Concat(GetLargeCities(con)).Concat(GetSmallCities(con)).ToList();
        }

        private static IEnumerable<Address> GetBucharest(OleDbConnection con)
        {
            using (var adapter = new OleDbDataAdapter("select * from [Bucuresti$]", con))
            {
                var table = new DataTable();
                adapter.Fill(table);

                return table.Rows.Cast<DataRow>().Select(address => new Address
                {
                    StreetType = address.ItemArray[0].ToString().ToStreetType(),
                    Street = address.ItemArray[1].ToString(),
                    NumberOrStair = address.ItemArray[2].ToString(),
                    ZipCode = address.ItemArray[3].ToString(),
                    City = "Bucuresti",
                    Region = "Sector " + address.ItemArray[4]
                });
            }
        }

        private static IEnumerable<Address> GetLargeCities(OleDbConnection con)
        {
            using (var adapter = new OleDbDataAdapter("select * from [Localitati > 50.000 loc$]", con))
            {
                var table = new DataTable();
                adapter.Fill(table);

                return table.Rows.Cast<DataRow>().Select(address => new Address
                {
                    StreetType = address.ItemArray[2].ToString().ToStreetType(),
                    Street = address.ItemArray[3].ToString(),
                    NumberOrStair = address.ItemArray[4].ToString(),
                    ZipCode = address.ItemArray[5].ToString(),
                    City = address.ItemArray[1].ToString(),
                    Region = address.ItemArray[0].ToString()
                });
            }
        }

        private static IEnumerable<Address> GetSmallCities(OleDbConnection con)
        {
            using (var adapter = new OleDbDataAdapter("select * from [Localitati < 50.000 loc$]", con))
            {
                var table = new DataTable();
                adapter.Fill(table);

                return table.Rows.Cast<DataRow>().Select(address => new Address
                {
                    StreetType = StreetType.None,
                    Street = null,
                    NumberOrStair = null,
                    ZipCode = address.ItemArray[2].ToString(),
                    City = address.ItemArray[1].ToString(),
                    Region = address.ItemArray[0].ToString()
                });
            }
        }
    }
}


text:
package org.scalatra

import skinny.micro.{ SkinnyMicroServlet, ApiFormats }
import skinny.micro.routing.{ RailsPathPatternParser, RouteMatcher }

import scala.language.implicitConversions

import org.scalatra.test.specs2.MutableScalatraSpec

class ApiFormatsServlet extends SkinnyMicroServlet with ApiFormats {
  override protected implicit def string2RouteMatcher(path: String): RouteMatcher = RailsPathPatternParser(path)

  get("/hello(.:format)") {
    format
  }

  get("/hi") {
    format
  }

}

class ApiFormatsSpec extends MutableScalatraSpec {

  addServlet(new ApiFormatsServlet, "/*")

  "The ApiFormats" should {
    "get the format from the params" in {
      get("/hello.json") {
        val b = body
        response.getContentType must startWith("application/json")
        b must_== "json"
      }
    }

    "get the default format when no param has been given" in {
      get("/hello") {
        response.getContentType must startWith("text/html")
        body must_== "html"
      }
    }

    "get the format from a query string param" in {
      get("/hi?format=json") {
        response.getContentType must startWith("application/json")
        body must_== "json"
      }
    }

    "get the format from the accept header" in {
      "when there is only one format" in {
        get("/hello", headers = Map("Accept" -> "application/xml")) {
          response.getContentType must startWith("application/xml")
          body must_== "xml"
        }
      }

      "when the format is */*" in {
        get("/hello.xml", headers = Map("Accept" -> "*/*")) {
          response.getContentType must startWith("application/xml")
          body must_== "xml"
        }
      }

      "when there are multiple formats take the first match" in {
        get("/hello", headers = Map("Accept" -> "application/json, application/xml, text/plain, */*")) {
          response.getContentType must startWith("application/json")
          body must_== "json"
        }
      }

      "when there are multiple formats with priority take the first one with the highest weight" in {
        get("/hello", headers = Map("Accept" -> "application/json; q=0.4, application/xml; q=0.8, text/plain, */*")) {
          response.getContentType must startWith("text/plain")
          body must_== "txt"
        }
      }

      "when there is a content type which contains the default format, it should match" in {
        get("/hello", headers = Map("Content-Type" -> "application/xml,application/xhtml+xml,text/html")) {
          response.getContentType must startWith("text/html")
          body must startWith("html")
        }
      }

      "(#406) when there are multiple formats with malformed priority the parser should ignore the broken priority " in {
        get("/hello", headers = Map("Accept" -> "application/json; q=, application/xml; q=, text/plain, */*")) {
          response.getContentType must startWith("text/plain")
          body must_== "txt"
        }
      }
    }
  }
}


text:
#include <string>
#include <vector>
#include "library/time_task.h"
#include "library/profiling.h"
#include "library/library_task_builder.h"
#include "library/sorry.h"
#include "util/timeit.h"
#include "kernel/type_checker.h"
#include "kernel/declaration.h"
#include "kernel/instantiate.h"
#include "kernel/replace_fn.h"
#include "kernel/kernel_exception.h"
#include "library/trace.h"
#include "library/constants.h"
#include "library/explicit.h"
#include "library/typed_expr.h"
#include "library/private.h"
#include "library/protected.h"
#include "library/scoped_ext.h"
#include "library/unfold_macros.h"
#include "library/noncomputable.h"
#include "library/module.h"
#include "library/aux_definition.h"
#include "library/documentation.h"
#include "kernel/scope_pos_info_provider.h"
#include "library/replace_visitor.h"
#include "library/equations_compiler/util.h"
#include "library/equations_compiler/equations.h"
#include "library/compiler/vm_compiler.h"
#include "library/compiler/rec_fn_macro.h"
#include "library/tactic/eqn_lemmas.h"
#include "frontends/lean/parser.h"
#include "frontends/lean/tokens.h"
#include "frontends/lean/elaborator.h"
#include "frontends/lean/util.h"
#include "frontends/lean/decl_util.h"
#include "frontends/lean/decl_attributes.h"
#include "frontends/lean/definition_cmds.h"

namespace lean {
environment ensure_decl_namespaces(environment const & env, name const & full_n) {
    if (full_n.is_atomic())
        return env;
    return add_namespace(env, full_n.get_prefix());
}

expr parse_equation_lhs(parser & p, expr const & fn, buffer<expr> & locals) {
    auto lhs_pos = p.pos();
    buffer<expr> lhs_args;
    lhs_args.push_back(p.parse_pattern_or_expr(get_max_prec()));
    while (!p.curr_is_token(get_assign_tk())) {
        auto pos0 = p.pos();
        lhs_args.push_back(p.parse_pattern_or_expr(get_max_prec()));
        if (p.pos() == pos0) break;
    }
    expr lhs = p.mk_app(p.save_pos(mk_explicit(fn), lhs_pos), lhs_args, lhs_pos);
    bool skip_main_fn = true;
    return p.patexpr_to_pattern(lhs, skip_main_fn, locals);
}

expr parse_equation(parser & p, expr const & fn) {
    p.check_token_next(get_bar_tk(), "invalid equation, '|' expected");
    buffer<expr> locals;
    expr lhs = parse_equation_lhs(p, fn, locals);
    auto assign_pos = p.pos();
    p.check_token_next(get_assign_tk(), "invalid equation, ':=' expected");
    expr rhs = p.parse_scoped_expr(locals);
    return Fun(locals, p.save_pos(mk_equation(lhs, rhs), assign_pos), p);
}

optional<expr> parse_using_well_founded(parser & p) {
    if (p.curr_is_token(get_using_well_founded_tk())) {
        parser::local_scope _(p);
        p.clear_expr_locals();
        p.next();
        return some_expr(p.parse_expr(get_max_prec()));
    } else {
        return none_expr();
    }
}

expr mk_equations(parser & p, buffer<expr> const & fns,
                  buffer<name> const & fn_full_names, buffer<name> const & fn_prv_names, buffer<expr> const & eqs,
                  optional<expr> const & wf_tacs, pos_info const & pos) {
    buffer<expr> new_eqs;
    for (expr const & eq : eqs) {
        new_eqs.push_back(Fun(fns, eq, p));
    }
    equations_header h = mk_equations_header(to_list(fn_full_names), to_list(fn_prv_names));
    if (wf_tacs) {
        return p.save_pos(mk_equations(h, new_eqs.size(), new_eqs.data(), *wf_tacs), pos);
    } else {
        return p.save_pos(mk_equations(h, new_eqs.size(), new_eqs.data()), pos);
    }
}

expr mk_equations(parser & p, expr const & fn, name const & full_name, name const & full_prv_name, buffer<expr> const & eqs,
                  optional<expr> const & wf_tacs, pos_info const & pos) {
    buffer<expr> fns; fns.push_back(fn);
    buffer<name> full_names; full_names.push_back(full_name);
    buffer<name> full_prv_names; full_prv_names.push_back(full_prv_name);
    return mk_equations(p, fns, full_names, full_prv_names, eqs, wf_tacs, pos);
}

void check_valid_end_of_equations(parser & p) {
    if (!p.curr_is_command() && !p.curr_is_eof() &&
        p.curr() != token_kind::DocBlock &&
        p.curr() != token_kind::ModDocBlock &&
        !p.curr_is_token(get_with_tk()) &&
        !p.curr_is_token(get_period_tk())) {
        p.maybe_throw_error({"invalid equations, must be followed by a command, '.', 'with', doc-string or EOF", p.pos()});
    }
}

static expr parse_mutual_definition(parser & p, buffer<name> & lp_names, buffer<expr> & fns, buffer<name> & prv_names,
                                    buffer<expr> & params) {
    parser::local_scope scope1(p);
    auto header_pos = p.pos();
    buffer<expr> pre_fns;
    parse_mutual_header(p, lp_names, pre_fns, params);
    buffer<expr> eqns;
    buffer<name> full_names;
    buffer<name> full_actual_names;
    for (expr const & pre_fn : pre_fns) {
        // TODO(leo, dhs): make use of attributes
        expr fn_type = parse_inner_header(p, mlocal_pp_name(pre_fn)).first;
        declaration_name_scope scope2(mlocal_pp_name(pre_fn));
        declaration_name_scope scope3("_main");
        full_names.push_back(scope3.get_name());
        full_actual_names.push_back(scope3.get_actual_name());
        prv_names.push_back(scope2.get_actual_name());
        if (p.curr_is_token(get_period_tk())) {
            auto period_pos = p.pos();
            p.next();
            eqns.push_back(p.save_pos(mk_no_equation(), period_pos));
        } else {
            while (p.curr_is_token(get_bar_tk())) {
                eqns.push_back(parse_equation(p, pre_fn));
            }
            check_valid_end_of_equations(p);
        }
        expr fn      = mk_local(mlocal_name(pre_fn), mlocal_pp_name(pre_fn), fn_type, mk_rec_info(true));
        fns.push_back(fn);
    }
    if (p.curr_is_token(get_with_tk()))
        p.maybe_throw_error({"unexpected 'with' clause", p.pos()});
    optional<expr> wf_tacs = parse_using_well_founded(p);
    for (expr & eq : eqns) {
        eq = replace_locals_preserving_pos_info(eq, pre_fns, fns);
    }
    expr r = mk_equations(p, fns, full_names, full_actual_names, eqns, wf_tacs, header_pos);
    collect_implicit_locals(p, lp_names, params, r);
    return r;
}

static void finalize_definition(elaborator & elab, buffer<expr> const & params, expr & type,
                                expr & val, buffer<name> & lp_names, bool is_meta) {
    type = elab.mk_pi(params, type);
    val  = elab.mk_lambda(params, val);
    buffer<expr> type_val;
    buffer<name> implicit_lp_names;
    type_val.push_back(type);
    type_val.push_back(val);
    elab.finalize(type_val, implicit_lp_names, true, false);
    if (!is_meta) {
        type = unfold_untrusted_macros(elab.env(), type_val[0]);
        val  = unfold_untrusted_macros(elab.env(), type_val[1]);
    } else {
        type = type_val[0];
        val  = type_val[1];
    }
    lp_names.append(implicit_lp_names);
}

static certified_declaration check(parser & p, environment const & env, name const & c_name, declaration const & d, pos_info const & pos) {
    try {
        time_task _("type checking", p.mk_message(pos, INFORMATION), p.get_options(), c_name);
        return ::lean::check(env, d);
    } catch (kernel_exception & ex) {
        unsigned i = get_pp_indent(p.get_options());
        auto pp_fn = ::lean::mk_pp_ctx(env, p.get_options(), metavar_context(), local_context());
        format msg = format("kernel failed to type check declaration '") + format(c_name) + format("' ") +
            format("this is usually due to a buggy tactic or a bug in the builtin elaborator");
        msg += line() + format("elaborated type:");
        msg += nest(i, line() + pp_fn(d.get_type()));
        if (d.is_definition()) {
            msg += line() + format("elaborated value:");
            msg += nest(i, line() + pp_fn(d.get_value()));
        }
        throw nested_exception(msg, ex);
    }
}

static bool check_noncomputable(bool ignore_noncomputable, environment const & env, name const & c_name, name const & c_real_name, bool is_noncomputable,
                                std::string const & file_name, pos_info const & pos) {
    if (ignore_noncomputable)
        return true;
    if (!is_noncomputable && is_marked_noncomputable(env, c_real_name)) {
        auto reason = get_noncomputable_reason(env, c_real_name);
        lean_assert(reason);
        report_message(message(file_name, pos, ERROR,
                               (sstream() << "definition '" << c_name << "' is noncomputable, it depends on '" << *reason << "'").str()));
        return false;
    }
    if (is_noncomputable && !is_marked_noncomputable(env, c_real_name)) {
        report_message(message(file_name, pos, WARNING,
                               (sstream() << "definition '" << c_name << "' was incorrectly marked as noncomputable").str()));
    }
    return true;
}

static environment compile_decl(parser & p, environment const & env,
                                name const & c_name, name const & c_real_name, pos_info const & pos) {
    try {
        time_task _("compilation", p.mk_message(message_severity::INFORMATION), p.get_options(), c_real_name);
        return vm_compile(env, env.get(c_real_name));
    } catch (exception & ex) {
        // FIXME(gabriel): use position from exception
        auto out = p.mk_message(pos, WARNING);
        out << "failed to generate bytecode for '" << c_name << "'\n";
        out.set_exception(ex);
        out.report();
        return env;
    }
}

static pair<environment, name>
declare_definition(parser & p, environment const & env, decl_cmd_kind kind, buffer<name> const & lp_names,
                   name const & c_name, name const & prv_name, expr type, optional<expr> val, task<expr> const & proof, cmd_meta const & meta,
                   bool is_abbrev, pos_info const & pos) {
    name c_real_name;
    environment new_env = env;
    if (has_private_prefix(new_env, prv_name)) {
        new_env     = register_private_name(new_env, c_name, prv_name);
        c_real_name = prv_name;
    } else {
        c_real_name = get_namespace(env) + c_name;
    }
    if (env.find(c_real_name)) {
        throw exception(sstream() << "invalid definition, a declaration named '" << c_real_name << "' has already been declared");
    }
    if (val && !meta.m_modifiers.m_is_meta && !type_checker(env).is_prop(type)) {
        /* We only abstract nested proofs if the type of the definition is not a proposition */
        std::tie(new_env, type) = abstract_nested_proofs(new_env, c_real_name, type);
        std::tie(new_env, *val) = abstract_nested_proofs(new_env, c_real_name, *val);
    }
    bool use_conv_opt = true;
    bool is_trusted   = !meta.m_modifiers.m_is_meta;
    auto def          =
        !val ? mk_theorem(c_real_name, to_list(lp_names), type, proof) :
        (kind == decl_cmd_kind::Theorem ?
         mk_theorem(c_real_name, to_list(lp_names), type, *val) :
         (is_abbrev ? mk_definition(c_real_name, to_list(lp_names), type, *val, reducibility_hints::mk_abbreviation(), is_trusted) :
          mk_definition(new_env, c_real_name, to_list(lp_names), type, *val, use_conv_opt, is_trusted)));
    auto cdef         = check(p, new_env, c_name, def, pos);
    new_env           = module::add(new_env, cdef);

    check_noncomputable(p.ignore_noncomputable(), new_env, c_name, c_real_name, meta.m_modifiers.m_is_noncomputable, p.get_file_name(), pos);

    if (meta.m_modifiers.m_is_protected)
        new_env = add_protected(new_env, c_real_name);

    new_env = add_alias(new_env, meta.m_modifiers.m_is_protected, c_name, c_real_name);

    if (!meta.m_modifiers.m_is_private) {
        new_env = ensure_decl_namespaces(new_env, c_real_name);
    }

    new_env = compile_decl(p, new_env, c_name, c_real_name, pos);
    if (meta.m_doc_string) {
        new_env = add_doc_string(new_env, c_real_name, *meta.m_doc_string);
    }
    return mk_pair(new_env, c_real_name);
}

static void throw_unexpected_error_at_copy_lemmas() {
    throw exception("unexpected error, failed to generate equational lemmas in the front-end");
}

/* Given e of the form Pi (a_1 : A_1) ... (a_n : A_n), lhs = rhs,
   return the pair (lhs, n) */
static pair<expr, unsigned> get_lemma_lhs(expr e) {
    unsigned nparams = 0;
    while (is_pi(e)) {
        nparams++;
        e = binding_body(e);
    }
    expr lhs, rhs;
    if (!is_eq(e, lhs, rhs))
        throw_unexpected_error_at_copy_lemmas();
    return mk_pair(lhs, nparams);
}

/*
   Given a lemma with parameters lp_names:
      [lp_1 ... lp_n]
   and the levels in the function application on the lemma left-hand-side lhs_fn_levels:
      [u_1 ... u_n] s.t. there is a permutation p s.t. p([u_1 ... u_n] = [(mk_univ_param lp_1) ... (mk_univ_param lp_n)],
   and levels fn_levels
      [v_1 ... v_n]
   Then, store
      p([v_1 ... v_n])
   in result.
*/
static void get_levels_for_instantiating_lemma(level_param_names const & lp_names,
                                               levels const & lhs_fn_levels,
                                               levels const & fn_levels,
                                               buffer<level> & result) {
    buffer<level> fn_levels_buffer;
    buffer<level> lhs_fn_levels_buffer;
    to_buffer(fn_levels, fn_levels_buffer);
    to_buffer(lhs_fn_levels, lhs_fn_levels_buffer);
    lean_assert(fn_levels_buffer.size() == lhs_fn_levels_buffer.size());
    for (name const & lp_name : lp_names) {
        unsigned j = 0;
        for (; j < lhs_fn_levels_buffer.size(); j++) {
            if (!is_param(lhs_fn_levels_buffer[j])) throw_unexpected_error_at_copy_lemmas();
            if (param_id(lhs_fn_levels_buffer[j]) == lp_name) {
                result.push_back(fn_levels_buffer[j]);
                break;
            }
        }
        lean_assert(j < lhs_fn_levels_buffer.size());
    }
}

/**
   Given a lemma with the given arity (i.e., number of nested Pi-terms),
   n = args.size() <= lhs_args.size(), and the first n
   arguments in lhs_args.size() are a permutation p of
     (var #0) ... (var #n-1)
   Then, store in result p(args)
*/
static void get_args_for_instantiating_lemma(unsigned arity,
                                             buffer<expr> const & lhs_args,
                                             buffer<expr> const & args,
                                             buffer<expr> & result) {
    for (unsigned i = 0; i < args.size(); i++) {
        if (!is_var(lhs_args[i]) || var_idx(lhs_args[i]) >= arity)
            throw_unexpected_error_at_copy_lemmas();
        result.push_back(args[arity - var_idx(lhs_args[i]) - 1]);
    }
}

/**
   Given declarations d_1, ..., d_n defined as
     (fun (a_1 : A_1) ... (a_n : A_n), d_1._main a_1' ... a_n')
   where a_1' ... a_n' is a permutation of a subset of a_1 ... a_n.
   Moreover, the parameters A_1 ... A_n are the same in all d_i's.
   Then, copy the equation lemmas from d._main to d.
*/
static environment copy_equation_lemmas(environment const & env, buffer<name> const & d_names) {
    type_context_old ctx(env, transparency_mode::All);
    type_context_old::tmp_locals locals(ctx);
    level_param_names lps;
    levels ls;
    buffer<expr> vals;
    buffer<expr> new_vals;
    for (unsigned d_idx = 0; d_idx < d_names.size(); d_idx++) {
        declaration const & d = env.get(d_names[d_idx]);
        expr val;
        if (d_idx == 0) {
            lps = d.get_univ_params();
            ls  = param_names_to_levels(lps);
            val = instantiate_value_univ_params(d, ls);
            while (is_lambda(val)) {
                expr local = locals.push_local_from_binding(val);
                val = instantiate(binding_body(val), local);
            }
        } else {
            val   = instantiate_value_univ_params(d, ls);
            for (expr const & local : locals.as_buffer()) {
                lean_assert(is_lambda(val));
                lean_assert(ctx.is_def_eq(ctx.infer(local), binding_domain(val)));
                val = instantiate(binding_body(val), local);
            }
        }
        buffer<expr> args;
        expr const & fn = get_app_args(val, args);
        if (!is_constant(fn) ||
            !std::all_of(args.begin(), args.end(), is_local)) {
            throw_unexpected_error_at_copy_lemmas();
        }
        vals.push_back(val);
        /* We want to create new equations where we replace val with new_val in the equations
           associated with fn. */
        expr new_val = mk_app(mk_constant(d_names[d_idx], ls), locals.as_buffer());
        new_vals.push_back(new_val);
    }
    /* Copy equations */
    environment new_env = env;
    for (unsigned d_idx = 0; d_idx < d_names.size(); d_idx++) {
        buffer<expr> args;
        expr const & fn = get_app_args(vals[d_idx], args);
        unsigned i = 1;
        while (true) {
            name eqn_name = mk_equation_name(const_name(fn), i);
            optional<declaration> eqn_decl = env.find(eqn_name);
            if (!eqn_decl) break;
            expr lhs; unsigned num_eqn_params;
            std::tie(lhs, num_eqn_params) = get_lemma_lhs(eqn_decl->get_type());
            buffer<expr> lhs_args;
            expr const & lhs_fn = get_app_args(lhs, lhs_args);
            if (!is_constant(lhs_fn) || const_name(lhs_fn) != const_name(fn) || lhs_args.size() < args.size())
                throw_unexpected_error_at_copy_lemmas();
            /* Get levels for instantiating the lemma */
            buffer<level> eqn_level_buffer;
            get_levels_for_instantiating_lemma(eqn_decl->get_univ_params(),
                                               const_levels(lhs_fn),
                                               const_levels(fn),
                                               eqn_level_buffer);
            levels eqn_levels = to_list(eqn_level_buffer);
            /* Get arguments for instantiating the lemma */
            buffer<expr> eqn_args;
            get_args_for_instantiating_lemma(num_eqn_params, lhs_args, args, eqn_args);
            /* Convert type */
            expr eqn_type = instantiate_type_univ_params(*eqn_decl, eqn_levels);
            for (unsigned j = 0; j < eqn_args.size(); j++) eqn_type = binding_body(eqn_type);
            eqn_type = instantiate_rev(eqn_type, eqn_args);
            expr new_eqn_type = replace(eqn_type, [&](expr const & e, unsigned) {
                    for (unsigned i = 0; i < vals.size(); i++) {
                        if (e == vals[i])
                            return some_expr(new_vals[i]);
                    }
                    return none_expr();
                });
            new_eqn_type = locals.mk_pi(new_eqn_type);
            name new_eqn_name    = mk_equation_name(d_names[d_idx], i);
            expr new_eqn_value;
            new_eqn_value = mk_app(mk_constant(eqn_name, eqn_levels), args);
            new_eqn_value = locals.mk_lambda(new_eqn_value);
            declaration new_decl = mk_theorem(new_eqn_name, lps, new_eqn_type, new_eqn_value);
            new_env = module::add(new_env, check(new_env, new_decl, true));
            if (is_rfl_lemma(env, eqn_name))
                new_env = mark_rfl_lemma(new_env, new_eqn_name);
            new_env = add_eqn_lemma(new_env, new_eqn_name);
            i++;
        }
    }
    return new_env;
}

/**
   Given a declaration d defined as
     (fun (a_1 : A_1) ... (a_n : A_n), d._main a_1' ... a_n')
   where a_1' ... a_n' is a permutation of a subset of a_1 ... a_n.
   Then, copy the equation lemmas from d._main to d.
*/
static environment copy_equation_lemmas(environment const & env, name const & d_name) {
    buffer<name> d_names;
    d_names.push_back(d_name);
    return copy_equation_lemmas(env, d_names);
}

static environment mutual_definition_cmd_core(parser & p, decl_cmd_kind kind, cmd_meta const & meta) {
    buffer<name> lp_names;
    buffer<expr> fns, params;
    declaration_info_scope scope(p, kind, meta.m_modifiers);
    auto header_pos = p.pos();
    /* TODO(Leo): allow a different doc string for each function in a mutual definition. */
    optional<std::string> doc_string = meta.m_doc_string;
    environment env = p.env();
    private_name_scope prv_scope(meta.m_modifiers.m_is_private, env);
    buffer<name> prv_names;
    expr val = parse_mutual_definition(p, lp_names, fns, prv_names, params);

    // skip elaboration of definitions during reparsing
    if (p.get_break_at_pos())
        return p.env();

    bool recover_from_errors = true;
    elaborator elab(env, p.get_options(), get_namespace(env) + mlocal_pp_name(fns[0]), metavar_context(), local_context(), recover_from_errors);
    buffer<expr> new_params;
    elaborate_params(elab, params, new_params);
    val = replace_locals_preserving_pos_info(val, params, new_params);
    val = elab.elaborate(val);
    if (!is_equations_result(val)) {
        /* Failed to elaborate mutual recursion.
           TODO(Leo): better error recovery. */
        return p.env();
    }
    unsigned num_defs = get_equations_result_size(val);
    lean_assert(num_defs == prv_names.size());
    lean_assert(fns.size() == num_defs);
    buffer<name> new_d_names;
    /* Define functions */
    for (unsigned i = 0; i < num_defs; i++) {
        expr curr      = get_equations_result(val, i);
        expr curr_type = head_beta_reduce(elab.infer_type(curr));
        finalize_definition(elab, new_params, curr_type, curr, lp_names, meta.m_modifiers.m_is_meta);
        environment env = elab.env();
        name c_name = mlocal_name(fns[i]);
        name c_real_name;
        bool is_abbrev = false;
        std::tie(env, c_real_name) = declare_definition(p, env, kind, lp_names, c_name, prv_names[i],
                                                        curr_type, some_expr(curr), {}, meta, is_abbrev, header_pos);
        env = add_local_ref(p, env, c_name, c_real_name, lp_names, params);
        new_d_names.push_back(c_real_name);
        elab.set_env(env);
    }
    /* Add lemmas */
    elab.set_env(copy_equation_lemmas(elab.env(), new_d_names));
    /* Apply attributes last so that they may access any information on the new decl */
    for (auto const & c_real_name : new_d_names) {
        elab.set_env(meta.m_attrs.apply(elab.env(), p.ios(), c_real_name));
    }
    return elab.env();
}

/* Return tuple (fn, val, actual_name) where

   - fn is a local constant with the user name + type
   - val is the actual definition
   - actual_name for the kernel declaration.
     Note that mlocal_pp_name(fn) and actual_name are different for scoped/private declarations.
*/
static std::tuple<expr, expr, name> parse_definition(parser & p, buffer<name> & lp_names, buffer<expr> & params,
                                                     bool is_example, bool is_instance, bool is_meta, bool is_abbrev) {
    parser::local_scope scope1(p);
    auto header_pos = p.pos();
    time_task _("parsing", p.mk_message(header_pos, INFORMATION), p.get_options());
    declaration_name_scope scope2;
    expr fn = parse_single_header(p, scope2, lp_names, params, is_example, is_instance);
    expr val;
    if (p.curr_is_token(get_assign_tk())) {
        p.next();
        if (is_meta) {
            declaration_name_scope scope2("_main");
            fn = mk_local(mlocal_name(fn), mlocal_pp_name(fn), mlocal_type(fn), mk_rec_info(true));
            p.add_local(fn);
            val = p.parse_expr();
            /* add fake equation */
            expr eqn = copy_tag(val, mk_equation(fn, val));
            buffer<expr> eqns;
            eqns.push_back(eqn);
            val = mk_equations(p, fn, scope2.get_name(), scope2.get_actual_name(), eqns, {}, header_pos);
        } else {
            val = p.parse_expr();
        }
    } else if (p.curr_is_token(get_bar_tk()) || p.curr_is_token(get_period_tk())) {
        if (is_abbrev)
            throw exception("invalid abbreviation, abbreviations should not be defined using pattern matching");
        declaration_name_scope scope2("_main");
        fn = mk_local(mlocal_name(fn), mlocal_pp_name(fn), mlocal_type(fn), mk_rec_info(true));
        p.add_local(fn);
        buffer<expr> eqns;
        if (p.curr_is_token(get_period_tk())) {
            auto period_pos = p.pos();
            p.next();
            eqns.push_back(p.save_pos(mk_no_equation(), period_pos));
        } else {
            while (p.curr_is_token(get_bar_tk())) {
                eqns.push_back(parse_equation(p, fn));
            }
            check_valid_end_of_equations(p);
        }
        optional<expr> wf_tacs = parse_using_well_founded(p);
        val = mk_equations(p, fn, scope2.get_name(), scope2.get_actual_name(), eqns, wf_tacs, header_pos);
    } else {
        val = p.parser_error_or_expr({"invalid definition, '|' or ':=' expected", p.pos()});
    }
    collect_implicit_locals(p, lp_names, params, {mlocal_type(fn), val});
    return std::make_tuple(fn, val, scope2.get_actual_name());
}

static void replace_params(buffer<expr> const & params, buffer<expr> const & new_params, expr & fn, expr & val) {
    expr fn_type = replace_locals_preserving_pos_info(mlocal_type(fn), params, new_params);
    expr new_fn  = update_mlocal(fn, fn_type);
    val          = replace_locals_preserving_pos_info(val, params, new_params);
    val          = replace_local_preserving_pos_info(val, fn, new_fn);
    fn           = new_fn;
}

static expr_pair elaborate_theorem(elaborator & elab, expr const & fn, expr val) {
    expr fn_type = elab.elaborate_type(mlocal_type(fn));
    elab.ensure_no_unassigned_metavars(fn_type);
    expr new_fn  = update_mlocal(fn, fn_type);
    val = replace_local_preserving_pos_info(val, fn, new_fn);
    return elab.elaborate_with_type(val, mk_as_is(fn_type));
}

static expr_pair elaborate_definition_core(elaborator & elab, decl_cmd_kind kind, expr const & fn, expr const & val) {
    if (kind == decl_cmd_kind::Theorem) {
        return elaborate_theorem(elab, fn, val);
    } else {
        return elab.elaborate_with_type(val, mlocal_type(fn));
    }
}

static expr_pair elaborate_definition(parser & p, elaborator & elab, decl_cmd_kind kind, expr const & fn, expr const & val, pos_info const & pos) {
    time_task _("elaboration", p.mk_message(pos, INFORMATION), p.get_options(), mlocal_pp_name(fn));
    return elaborate_definition_core(elab, kind, fn, val);
}

static void finalize_theorem_type(elaborator & elab, buffer<expr> const & params, expr & type,
                                  buffer<name> & lp_names,
                                  elaborator::theorem_finalization_info & info) {
    type = elab.mk_pi(params, type);
    buffer<name> implicit_lp_names;
    std::tie(type, info) = elab.finalize_theorem_type(type, implicit_lp_names);
    type = unfold_untrusted_macros(elab.env(), type);
    lp_names.append(implicit_lp_names);
}

static void finalize_theorem_proof(elaborator & elab, buffer<expr> const & params, expr & val,
                                   elaborator::theorem_finalization_info const & info) {
    val  = elab.mk_lambda(params, val);
    val = elab.finalize_theorem_proof(val, info);
    val = unfold_untrusted_macros(elab.env(), val);
}

static expr inline_new_defs(environment const & old_env, environment const & new_env, name const & n, expr const & e) {
    return replace(e, [=] (expr const & e, unsigned) -> optional<expr> {
        if (is_sorry(e)) {
            return none_expr();
        } else if (is_constant(e) && !old_env.find(const_name(e))) {
            auto decl = new_env.get(const_name(e));
            if (decl.is_definition()) {
                expr val  = instantiate_value_univ_params(decl, const_levels(e));
                lean_assert(decl.is_definition());
                return some_expr(inline_new_defs(old_env, new_env, n, val));
            } else {
                throw exception(sstream() << "invalid theorem '" << n << "', theorems should not depend on axioms introduced using "
                                "tactics (solution: mark theorem as a definition)");
            }
        } else {
            return none_expr();
        }
    });
}

static expr elaborate_proof(
        environment const & decl_env, options const & opts,
        pos_info const & header_pos,
        list<expr> const & params_list,
        expr const & fn, expr const & val0, elaborator::theorem_finalization_info const & finfo,
        bool is_rfl_lemma, expr const & final_type,
        metavar_context const & mctx, local_context const & lctx,
        parser_pos_provider pos_provider, bool use_info_manager, std::string const & file_name) {
    auto tc = std::make_shared<type_context_old>(decl_env, opts, mctx, lctx);
    scope_trace_env scope2(decl_env, opts, *tc);
    scope_traces_as_messages scope2a(file_name, header_pos);
    scope_pos_info_provider scope3(pos_provider);
    auto_reporting_info_manager_scope scope4(file_name, use_info_manager);

    try {
        bool recover_from_errors = true;
        elaborator elab(decl_env, opts, get_namespace(decl_env) + mlocal_pp_name(fn), mctx, lctx, recover_from_errors);

        expr val, type;
        {
            time_task _("elaboration", message_builder(tc, decl_env, get_global_ios(), file_name, header_pos, INFORMATION),
                        opts, mlocal_pp_name(fn));
            std::tie(val, type) = elab.elaborate_with_type(val0, mk_as_is(mlocal_type(fn)));
        }

        if (is_equations_result(val))
            val = get_equations_result(val, 0);
        buffer<expr> params; for (auto & e : params_list) params.push_back(e);
        finalize_theorem_proof(elab, params, val, finfo);
        if (is_rfl_lemma && !lean::is_rfl_lemma(final_type, val))
            throw exception("not a rfl-lemma, even though marked as rfl");
        return inline_new_defs(decl_env, elab.env(), mlocal_pp_name(fn), val);
    } catch (exception & ex) {
        /* Remark: we need the catch to be able to produce correct line information */
        message_builder(tc, decl_env, get_global_ios(), file_name, header_pos, ERROR)
            .set_exception(ex).report();
        return mk_sorry(final_type, true);
    }
}

static void check_example(environment const & decl_env, options const & opts,
                          decl_modifiers modifiers, bool noncomputable_theory,
                          level_param_names const & univ_params, list<expr> const & params,
                          expr const & fn, expr const & val0,
                          metavar_context const & mctx, local_context const & lctx,
                          parser_pos_provider pos_provider, bool use_info_manager, std::string const & file_name) {
    auto tc = std::make_shared<type_context_old>(decl_env, opts, mctx, lctx);
    scope_trace_env scope2(decl_env, opts, *tc);
    scope_traces_as_messages scope2a(file_name, pos_provider.get_some_pos());
    scope_pos_info_provider scope3(pos_provider);
    auto_reporting_info_manager_scope scope4(file_name, use_info_manager);
    module::scope_pos_info scope_pos(pos_provider.get_some_pos());

    name decl_name = "_example";

    try {
        bool recover_from_errors = true;
        elaborator elab(decl_env, opts, decl_name, mctx, lctx, recover_from_errors);

        expr val, type;
        std::tie(val, type) = elab.elaborate_with_type(val0, mlocal_type(fn));
        buffer<expr> params_buf; for (auto & p : params) params_buf.push_back(p);
        buffer<name> univ_params_buf; to_buffer(univ_params, univ_params_buf);
        finalize_definition(elab, params_buf, type, val, univ_params_buf, modifiers.m_is_meta);

        bool use_conv_opt = true;
        bool is_trusted  = !modifiers.m_is_meta;
        auto new_env = elab.env();
        auto def = mk_definition(new_env, decl_name, to_list(univ_params_buf), type, val, use_conv_opt, is_trusted);
        auto cdef = check(new_env, def);
        new_env = module::add(new_env, cdef);
        check_noncomputable(noncomputable_theory, new_env, decl_name, def.get_name(), modifiers.m_is_noncomputable,
                                 pos_provider.get_file_name(), pos_provider.get_some_pos());
    } catch (throwable & ex) {
        message_builder error_msg(tc, decl_env, get_global_ios(),
                                  pos_provider.get_file_name(), pos_provider.get_some_pos(),
                                  ERROR);
        error_msg.set_exception(ex);
        error_msg.report();
        throw;
    }
}

static bool is_rfl_preexpr(expr const & e) {
    return is_constant(e, get_rfl_name());
}

environment single_definition_cmd_core(parser & p, decl_cmd_kind kind, cmd_meta meta) {
    buffer<name> lp_names;
    buffer<expr> params;
    expr fn, val;
    auto header_pos = p.pos();
    module::scope_pos_info scope_pos(header_pos);
    declaration_info_scope scope(p, kind, meta.m_modifiers);
    environment env   = p.env();
    private_name_scope prv_scope(meta.m_modifiers.m_is_private, env);
    bool is_example   = (kind == decl_cmd_kind::Example);
    bool is_instance  = (kind == decl_cmd_kind::Instance);
    bool is_abbrev    = (kind == decl_cmd_kind::Abbreviation);
    bool aux_lemmas   = scope.gen_aux_lemmas();
    bool is_rfl       = false;
    if (is_instance)
        meta.m_attrs.set_attribute(env, "instance");
    if (is_abbrev) {
        meta.m_attrs.set_attribute(env, "inline");
        meta.m_attrs.set_attribute(env, "reducible");
    }
    name prv_name;
    std::tie(fn, val, prv_name) = parse_definition(p, lp_names, params, is_example, is_instance, meta.m_modifiers.m_is_meta, is_abbrev);

    auto begin_pos = p.cmd_pos();
    auto end_pos = p.pos();
    scope_log_tree lt(logtree().mk_child({}, (get_namespace(env) + mlocal_pp_name(fn)).to_string(),
                                         {logtree().get_location().m_file_name, {begin_pos, end_pos}}));

    // skip elaboration of definitions during reparsing
    if (p.get_break_at_pos())
        return p.env();

    bool recover_from_errors = true;
    elaborator elab(env, p.get_options(), get_namespace(env) + mlocal_pp_name(fn), metavar_context(), local_context(), recover_from_errors);
    buffer<expr> new_params;
    elaborate_params(elab, params, new_params);
    elab.freeze_local_instances();
    replace_params(params, new_params, fn, val);

    auto process = [&](expr val) -> environment {
        expr type;
        optional<expr> opt_val;
        bool eqns = false;
        name c_name = mlocal_name(fn);
        pair<environment, name> env_n;
        if (kind == decl_cmd_kind::Theorem) {
            is_rfl = is_rfl_preexpr(val);
            type = elab.elaborate_type(mlocal_type(fn));
            elab.ensure_no_unassigned_metavars(type);
            expr new_fn = update_mlocal(fn, type);
            val = replace_local_preserving_pos_info(val, fn, new_fn);
            elaborator::theorem_finalization_info thm_finfo;
            finalize_theorem_type(elab, new_params, type, lp_names, thm_finfo);
            auto decl_env = elab.env();
            auto opts = p.get_options();
            auto new_params_list = to_list(new_params);
            auto mctx = elab.mctx();
            auto lctx = elab.lctx();
            auto pos_provider = p.get_parser_pos_provider(header_pos);
            bool use_info_manager = get_global_info_manager() != nullptr;
            std::string file_name = p.get_file_name();
            auto proof = add_library_task(task_builder<expr>([=] {
                return elaborate_proof(decl_env, opts, header_pos, new_params_list,
                                       new_fn, val, thm_finfo, is_rfl, type,
                                       mctx, lctx, pos_provider, use_info_manager, file_name);
            }), log_tree::ElaborationLevel);
            env_n = declare_definition(p, elab.env(), kind, lp_names, c_name, prv_name, type, opt_val, proof, meta, is_abbrev, header_pos);
        } else if (kind == decl_cmd_kind::Example) {
            auto env = p.env();
            auto opts = p.get_options();
            auto lp_name_list = to_list(lp_names);
            auto new_params_list = to_list(new_params);
            auto mctx = elab.mctx();
            auto lctx = elab.lctx();
            auto pos_provider = p.get_parser_pos_provider(p.cmd_pos());
            bool use_info_manager = get_global_info_manager() != nullptr;
            bool noncomputable_theory = p.ignore_noncomputable();
            std::string file_name = p.get_file_name();
            add_library_task<unit>([=] {
                check_example(env, opts, meta.m_modifiers, noncomputable_theory,
                              lp_name_list, new_params_list, fn, val, mctx, lctx,
                              pos_provider, use_info_manager, file_name);
                return unit();
            }, log_tree::ElaborationLevel);
            return p.env();
        } else {
            std::tie(val, type) = elaborate_definition(p, elab, kind, fn, val, header_pos);
            eqns = is_equations_result(val);
            if (eqns) {
                lean_assert(is_equations_result(val));
                lean_assert(get_equations_result_size(val) == 1);
                val = get_equations_result(val, 0);
            }
            finalize_definition(elab, new_params, type, val, lp_names, meta.m_modifiers.m_is_meta);
            env_n = declare_definition(p, elab.env(), kind, lp_names, c_name, prv_name, type, some_expr(val), {}, meta, is_abbrev, header_pos);
        }
        time_task _("decl post-processing", p.mk_message(header_pos, INFORMATION), p.get_options(), c_name);
        environment new_env = env_n.first;
        name c_real_name    = env_n.second;
        if (is_rfl) new_env = mark_rfl_lemma(new_env, c_real_name);
        new_env = add_local_ref(p, new_env, c_name, c_real_name, lp_names, params);
        if (eqns && aux_lemmas) {
            new_env = copy_equation_lemmas(new_env, c_real_name);
        }
        if (!meta.m_modifiers.m_is_meta && (kind == decl_cmd_kind::Definition || kind == decl_cmd_kind::Instance)) {
            if (!eqns) {
                unsigned arity = new_params.size();
                new_env = mk_simple_equation_lemma_for(new_env, p.get_options(), meta.m_modifiers.m_is_private, c_name, c_real_name, arity);
            } else {
                new_env = mk_smart_unfolding_definition(new_env, p.get_options(), c_real_name);
            }
        }
        /* Apply attributes last so that they may access any information on the new decl */
        return meta.m_attrs.apply(new_env, p.ios(), c_real_name);
    };

    try {
        return process(val);
    } catch (throwable & ex) {
        // Even though we catch exceptions during elaboration, there can still be other exceptions,
        // e.g. when adding a declaration to the environment.

        // If we have already logged an error during elaboration, don't
        // bother showing the less helpful kernel exception
        if (!lt.get().has_entry_now(is_error_message))
            p.mk_message(header_pos, ERROR).set_exception(ex).report();
        // As a last resort, try replacing the definition body with `sorry`.
        // If that doesn't work either, just silently give up since we have
        // already logged at least one error.
        try {
            return process(p.mk_sorry(header_pos, true));
        } catch (...) {
            return env;
        }
    }
}

environment definition_cmd_core(parser & p, decl_cmd_kind kind, cmd_meta const & meta) {
    if (meta.m_modifiers.m_is_mutual)
        return mutual_definition_cmd_core(p, kind, meta);
    else
        return single_definition_cmd_core(p, kind, meta);
}
}


text:
===============================
Contributing to django Organice
===============================

Official repositories: (kept in sync)

#. `Bitbucket`:index:: https://bitbucket.org/organice/django-organice
#. `GitHub`:index:: https://github.com/Organice/django-organice

Fork any of the repositories, make your code changes or additions, and place a pull request.

How To Get Started
==================

After cloning your own fork from Bitbucket or GitHub make sure you have created and activated
a virtual environment for development, then run ``make develop`` to install packages that help
you with your development tasks (tools for testing, translation, docs generation).

Guidelines
----------

The primary interpreter target to develop against is **Python 3**.  Ideally, use the highest one the
`Django package`_ integrated into our project is compatible with (3.4, at the moment).  All other
supported Python versions are tested by the `integration server`_ as soon as you place the pull request.
You can run tests locally before pushing using ``tox`` or ``setup.py test``, e.g. ::

    $ tox               # run all tests against all supported Python versions
    $ tox -e py34,py27  # run all tests against Python 3.4 and 2.7 only
    $ ./setup.py -q test -a tests/management  # only run management tests against default python

Source code is supposed to satisfy ``flake8`` default rules (with the exception of line length,
which can be up to 120 characters long).  A pre-commit hook for Git is installed automatically
for your convenience when you run ``make develop``, so you shouldn't even be able to commit when
``flake8`` is not passing.  Additional static analysis is conducted by the `QA server`_, and you
should make sure that code health goes up (or stays the same) with each contribution.

.. _Django package: https://pypi.python.org/pypi/Django/1.8#downloads
.. _integration server: https://travis-ci.org/Organice/django-organice
.. _QA server: https://landscape.io/github/Organice/django-organice/master

Help Wanted
===========

- Writing tests (unit tests, BDD tests)
- New features on the roadmap (see README)
- Translations (blog posts, documentation, user interface)

Translation
-----------

Translation is done on `Transifex`_ for both the project text strings and the documentation.

The multilingual documentation is written in `reStructuredText`_ syntax, and built using `Sphinx`_.
As a `translator`:index: you can simply jump on `Transifex`:index: and get your hands dirty for
your language.  Some helpful background reading is available from `Read the Docs`_ and
`Transifex support`_.

.. _Transifex: https://www.transifex.com/projects/p/django-organice-docs/
.. _reStructuredText: http://docutils.sourceforge.net/docs/user/rst/quickref.html
.. _Sphinx: http://sphinx-doc.org/intl.html
.. _Read the Docs: http://read-the-docs.readthedocs.org/en/latest/i18n.html
.. _Transifex support: http://support.transifex.com/customer/portal/articles/972120-introduction-to-the-web-editor

Bumping Versions, Package, Release
==================================

As for the `version numbers`:index: of django Organice we use `Semantic Versioning`_.  Changes
from one to the next `release`:index: are documented in the :doc:`changelog`.

.. _Semantic Versioning: http://semver.org/


text:
package org.assertj.core.api.junit.jupiter;

import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.extension.ParameterResolutionException;
import org.junit.jupiter.api.extension.ParameterResolver;

class ExtensionInjector implements ParameterResolver {

  @Override
  public boolean supportsParameter(ParameterContext parameterContext,
                                   ExtensionContext extensionContext) throws ParameterResolutionException {
    return parameterContext.getParameter().getType() == ExtensionContext.class;
  }

  @Override
  public Object resolveParameter(ParameterContext parameterContext,
                                 ExtensionContext extensionContext) throws ParameterResolutionException {
    return extensionContext;
  }

}


text:
package main

import (
	"fmt"
	"math/rand"
	"time"
)

// START0 OMIT
type Message struct {
	str string
	wait chan bool // HL
}
// STOP0 OMIT

func main() {
	c := fanIn(boring("Joe"), boring("Ann")) // HL
// START1 OMIT
	for i := 0; i < 5; i++ {
		msg1 := <-c; fmt.Println(msg1.str)
		msg2 := <-c; fmt.Println(msg2.str)
		msg1.wait <- true
		msg2.wait <- true
	}
// STOP1 OMIT
	fmt.Println("You're all boring; I'm leaving.")
}

func boring(msg string) <-chan Message { // Returns receive-only channel of strings. // HL
	c := make(chan Message)
// START2 OMIT
	waitForIt := make(chan bool) // Shared between all messages.
// STOP2 OMIT
	go func() { // We launch the goroutine from inside the function. // HL
		for i := 0; ; i++ {
// START3 OMIT
			c <- Message{ fmt.Sprintf("%s: %d", msg, i), waitForIt }
			time.Sleep(time.Duration(rand.Intn(2e3)) * time.Millisecond)
			<-waitForIt
// STOP3 OMIT
		}
	}()
	return c // Return the channel to the caller. // HL
}

// START3 OMIT
func fanIn(inputs ... <-chan Message) <-chan Message { // HL
	c := make(chan Message)
	for i := range inputs {
		input := inputs[i] // New instance of 'input' for each loop.
		go func() { for { c <- <-input } }()
	}
	return c
}
// STOP3 OMIT


text:
package net.festivoice;

import java.util.*;
import java.lang.*;
import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class WebServerLoader extends HttpServlet
{
	private static IChannelManager channelManager;

	public void init() throws ServletException
	{
		ServletConfig config = getServletConfig();

		String path = config.getInitParameter("database");
		if(path != null) {
			try {
				ServerLogger.getInstance().enableDatabase(path);
			} catch (IOException e) {
				throw new ServletException(e);
			}
		}

		int stream_port = Integer.parseInt(System.getProperty("streamPort", "6900"));
		InetSocketAddress stream_bind = new InetSocketAddress(stream_port);

		int timeoutInterval = Integer.parseInt(System.getProperty("timeoutInterval", "1000"));
		int timeoutLimit = Integer.parseInt(System.getProperty("timeoutLimit", "2"));

		StepTimeoutChannelManager m = new StepTimeoutChannelManager(timeoutInterval, timeoutLimit);
		m.start();

		try {
			AbstractStreamServer srv = new UDPStreamServer(stream_bind, m);
			srv.start();
		} catch (SocketException e) {
			e.printStackTrace();
			throw new ServletException(e);
		}

		channelManager = m;
	}

	public static IChannelManager getChannelManager()
	{
		return channelManager;
	}
}